I want to know if its possible to store php variables in mysql database i currently have a text field which stores the value

<div class="row-2">
    <div class="titles">
        <?=$config['slogan']?>
    </div>
</div>

this is the slogan for the website that is in anther table which is got before this - if i try and echo all that out it puts html comments around it so i get <!--$config['slogan']-->

if i use str_replace i get nothing from it - is it actually possible to change that into PHP without to much fuss or do i have to find anther way?

This prints for me. Yes you can store the value(s) of config[] in a database.

<?php $config['slogan'] = "My Website Slogan"; ?>

<div class="row-2">
    <div class="titles">
        <?=$config['slogan']?>
    </div>
</div>

Try this one:-

<?php
/* This code will make a connection with database */
$con=mysql_connect("localhost","","");

/* Now, we select the database */
mysql_select_db("");

/* Now we will store the values submitted by form in variable */
$username=$_POST['username'];
$pass=$_POST['password'];
$title=$_POST['title'];
$first_name=$_POST['first_name'];
/* we are now encrypting password while using md5() function */
$password=md5($pass);
$confirm_password=$_POST['confirm_password'];

/* Now we will check if username is already in use or not */
$queryuser=mysql_query("SELECT * FROM Customer WHERE username='$username' ");
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0)
{ echo "Sorry, ".$username." is already been taken."; }
else {

/* now we will check if password and confirm password matched */
if($pass != $confirm_password)
{ echo "Password and confirm password fields were not matched"; }
else {

/* Now we will write a query to insert user details into database */
$insert_user=mysql_query("INSERT INTO Customer (username, password, title, first_name) VALUES ('$username', '$password', '$title', '$first_name')");

if($insert_user)
{ echo "Registration Succesfull"; }
else
{ echo "error in registration".mysql_error(); }

/* closing the if else statements */
}}

mysql_close($con);
?>

After you select data from database, you can use many functions to put html comments or not.
And then use "echo" to show it.

Member Avatar for diafol

Just a word of caution wrt short tags (<?=)

This used to be frowned upon as some hosts disabled the use of short tags and as you'd have no access to the php.ini file, you could have been stuck (required replacement of short tags to long form). However, php5.4+ should ensure the availability of these short tags. If you have to move hosts for whatever reason, check php version and if <5.4, check if they allow short tags.

For your issue - I'm assuming all your files containing php have the php extension, or if not you have statements in your htaccess file to process these other extensions (e.g. *.html) as *.php

Your post is a bit confusing.

I want to know if its possible to store php variables in mysql database i currently have a text field which stores the value

Are you saying that $config['slogan'] has come from your DB?

this is the slogan for the website that is in anther table which is got before this

Do you mean a form or DB table?

Do you mean a form or DB table?

I have 2 DB tables one called configuration and one called content - The configuration has the site name, title, slogan etc... and the content holds the page content

<div class="row-2">
    <div class="titles">
        <?=$config['slogan']?>
    </div>
</div>

is stored in the content table but i need to access the config['slogan'] from the configuration table...

Member Avatar for diafol

Have you placed the data into the $config variable?

I'm assuming that you've done the connection, database selection and run the query before this code?

If not, look up mysqli in the php.net manual to see how to get your info out. DOn't use mysql - it's soon to die.

Yes the configuration table is the first query and stored in the array config - its all working fine as it contains the site name which is being called fine,

Im not sure your getting what i mean - il post some of my code
$config = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM configuration")); first call to the database then next is all the header information E.g meta tags and title

Then called is the site content depending on the page i have the htaccess to rewrite the filename as /home/title-of-page to index.php?page=title-of-page etc...

Once the page has been called i have some code which selects the content depending on the /home/$

$url = mysqli_real_escape_string($con, getFile());
$getPages = mysqli_query($con,"SELECT * FROM pages WHERE title = '$url'");
$content = mysqli_fetch_array($getPages);

once in the database the title of the page gets shortened down into 30 chararacters and has str_replace " ", "-" so the final link to the webpages are www.website.com/home/first-page-content etc...

one page that i am calling i have this stored in that database

<div class="row-2">
    <div class="titles">
        <?=$config['slogan']?>
    </div>
</div>

and i need to change the <?=$config['slogan']?> into the actual slogan which is held in the array $content['main_content']

i have tried to use str_replace to replace it but have had no avail.

look up mysqli in the php.net manual to see how to get your info out. DOn't use mysql - it's soon to die.

this is my first website in mysqli im use to mysql and have hardly used mysqli before so its like a tutorial for me, i have had 1 million errors already working with it due to me forgetting to put the DB link before the query - or forgetting to put it in at all.

I have solved the problem if i change the php variable in the database to contain

<div class="row-2">
    <div class="titles">
        [slogan]
    </div>
</div>

Then str_replace will replace it for the actual variable as long as i have

$content['content'] = str_replace("[slogan]", $config['slogan'], $content['content']);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.