PHP Variable in MySQL db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

PHP Variable in MySQL db

Post by anjanesh »

I've a row from a db :
$row = mysql_fetch_assoc($res);
$row['something'] contains The number is $var

If $var is set to 5 in the php page I want
echo $row['something']; to output - The number is 5

I tried entering The number is {$var} in the db but no luck.
exec cannot be used here I guess.

I know this has been posted many times here - Just dont know what search terms to give.
Thanks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

eval() may be it.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

n00b Saibot wrote:eval() may be it.
Or str_replace("\$var",$var,$row['something']); .. depends if you know what variables are going to be i the database string.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a regular expression to handle it wouldn't be too hard either... this is getting into template stuff though, which honestly, should probably not have php things in them.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

eval() gives unexpected T_STRING
str_replace can be used for a particular case but I'll have various variables which I may not know.
I didnt realize there wasnt a method (function) for this. Guess I'll to actually code for this.
Fine. I asked this since $var in a string gets parsed so I was wondering if theres a method for string in db.
So this is the way phpBB and others parse - they have separate user defined function to parse them right ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phpBB among others use a template engine which has it's own syntax and parsing system for templated variables in strings.
Post Reply