Variable in database field (variable in variable)
I have a variable defined in the php page code, called $id.
I am also displaying a database field from a mysql database, called $text.
The $text database field needs to display and load the $id variable in it as well.
$result=mysql_query("SELECT text FROM testtable WHERE id='1');
What does the $text field need to contain when I insert it into the testtable to display the $id in it as well.
Say the data in $text field is: 'here is the text and the id is $id'
What code needs to go around the $id part to display properly?
I guess it is a variable in a variable?
echo "here is the text and the id is $id";
echo "here is the text and the id is " . $id;
for inserting, change echo to a variable name.
$newtext = "here is the text and the id is $id";
There are two basic ways to go about this - either use eval e.g.
or if like me you think eval is the spawn of satan use some form of regex replacement. If you literally just want variables $xxxxx then something like
$newtext = preg_replace('/\$(\w+)/e','$$1',$text);
If you want parsing of fuller PHP code then eval may be the better (still evil) way to go.
Thank you both for your help. DailWelsh, your eval method worked perfectly. You Rock!
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)