Jump to content

Archived

This topic is now archived and is closed to further replies.

weldonj

Using Php Variables Stored in Mysql Entries

Recommended Posts

I'm trying to figure out how to store php variables within mysql fields and then call these nested php variables.

Say I have mysql field "descriptio"n which is a bunch of text:
description="mostly text but i sometimes want  a php variable called $id and get its value"

but then when I try and echo this row in php:
echo $description; my output will be
"mostly text but i sometimes want  a php variable called $id and get its value"

I tried different things within mysql and the field description:
description="mostly text but i sometimes want  a php variable called <?php echo $id;?> and get its value"

but then myoutput is just: "mostly text but i sometimes want  a php variable called $id and get its value <?php echo $id;?> and get its value";

i can't believe i can't figure this out. I think eval might help me out but I've tried various combinations and had no luck.

Share this post


Link to post
Share on other sites
if you have PHP in a database, PHP will treat the PHP as text. It wont parse it. In order to parse it you'll have to use eval

Share this post


Link to post
Share on other sites
I'm not sure how to get eval to work.  Not all of my database entries will have php variables in them.

I do

return eval($description);

where description="database entry i want my $phpvariable;?> here";

and i keep getting database entry i want my $phpvariable?> here";

The docs on eval have been of no helps so if anyone could show me how to embed the php variable in my mysql database it would be appreciated.

Share this post


Link to post
Share on other sites
You'll want to do this:
[code=php:0]eval("echo \"$description\"");[/code]

In order for your PHP variables to be parsed

Share this post


Link to post
Share on other sites

Does it make a difference that description is getting called from a mysql database using myrow. Suppose in my database the field description is: This is a $test

If I pull from the database and do:
$test="real name";
$sql = "my select statement";
$result = mysql_query($sql) or die(mysql_error());
while ($myrow = mysql_fetch_array($result)) {
$description=$myrow["description"]; }
eval('echo $description;');

My output is "this is a $test"

however if I instead do this:
$test="real name";
//replacing the  $description=myrow["$description"] below;
$description="This is a $test"; 
eval('echo $description;');

then i get the correct "this is a real name"


So clearly the $description=myrow["$description"] line is screwing things up. Can anyone explain to me how to get around this? or why this is causing problems.

Thanks

Share this post


Link to post
Share on other sites
I can't see how that would make a difference, unless somehow it's getting stick in single-quotes, even through the eval(), which makes no sense.  But I'm not a PHP guy.

Share this post


Link to post
Share on other sites
Whats in your eval should be in double quotes, and the $description variable should be surrounded in double quotes too.

So your eval should be like this:
eval("echo \"$description;\"");

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.