Thursday, November 20, 2008

Pulling Variables From Database

Recently I was working with a database and needed a way to have variables on each page but each page needed different content. i know this can be done with templating but I didn't want to have to import a big templating software for this one thing. I did a few searches online and after a while of searching saw mention of PhP's eval function.

It was a bit tricky to get working but here is the result:

eval("\$string = \"$string\";");
echo $string;

$string is the result from the database and can contain a string like "we have $variable1 for your $variable2!". The eval function takes car of parsing the PhP variable...very useful.

Thought this might be useful to anyone caring to read =)

No comments: