i have a situation where i need to be able to store variables from mysql. the pages i am creating are dynamic and contain different user data, what im chasing is the following
//stored in user database
$firstname = "BOB"
$lastname = "MARLEY"
//how i want th content from a mysql database to display in html
hello my name is BOB MARLEY
//how it displays at the moment
hello my name is $firstname $lastname
i cant seem to work it out at all
this is my current database code
<?php
$firstname= "BOB";
$lastnamename= "marley";
$get_content= mysqli_query($con,"SELECT * FROM tbl_forms_content WHERE forms_content_formid='1' LIMIT 1 ");
while($found_content = mysqli_fetch_array($get_content))
{
echo $found_content['forms_content_content'];
}
?>
any help is greatly apreciated
The right solution is to use a template library. To do what you're trying, you need to use eval
:
$firstname= "BOB";
$lastname= "marley";
while($found_content = mysqli_fetch_array($get_content))
{
eval ('echo "' . $found_content['forms_content_content'] . '";');
}
This is very dangerous. If the database contains values that can be set by the user, they could execute any PHP code from it.
The simplest way would be to str_replace them out;
echo str_replace(array('$firstname', '$lastname'), array($firstname, $lastname), $found_content['forms_content_content']);
Can see a working example here; http://codepad.org/xFEowbMZ