Jump to content


Photo

Using Php Variables Stored in Mysql Entries


  • Please log in to reply
6 replies to this topic

#1 weldonj

weldonj
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 August 2006 - 08:33 PM

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.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 August 2006 - 08:40 PM

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

#3 weldonj

weldonj
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 August 2006 - 11:57 PM

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.



#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 August 2006 - 12:53 PM

You'll want to do this:
eval("echo \"$description\"");

In order for your PHP variables to be parsed

#5 weldonj

weldonj
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 05 September 2006 - 05:48 PM


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


#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 September 2006 - 06:22 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 September 2006 - 09:43 PM

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;\"");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users