[thelist] php variables in mysql output

Joe Crawford joe at artlung.com
Mon Dec 4 08:58:26 CST 2000


Liz Lawson wrote:
> I want to hold some snippets of html in a mysql db (used to vary messages /
> images depending on date rather than using hardcoded "if else else"), but I
> need them to include PHP variable names. If I just echo out the result the
> variable name is printed as a string ($foo is just "$foo").

Take a look at:
	http://www.php.net/manual/function.eval.php
It should do what you desire.

>   I can see two ways of doing this:
> 1. Instead of holding the html in the db and doing $topgraphic = stuff from
> db, hold the names of include files which hold the actual html and use
> something like $topgraphic = include($filenamefromdb). I don't like this
> because of the possibility that a file specified in the db isn't there
> 
> 2. use str_replace() or similar to replace the variable name in the string
> with the variable value.
> 
> But I wondered if PHP already had the functionality for handling variables
> in db output, and I just don't know how to use it.

You really want to maximize your flexibility here - and be cautious to
only use variables which will exist. Depending on your needs you might
want to set the variables you expect to come out of the database to some
default before printing them to the screen.

Best,
	Joe
-- 
Joe Crawford <mailto:joe at artlung.com>
 * I am a Web Designer and Developer;
 * Find out about me at : http://www.ArtLung.com
 * A Mailing List for San Diego Web Folks : http://WebSanDiego.org




More information about the thelist mailing list