Web Hosting Talk







View Full Version : [PHP] Problem with eval() & MySQL


mouldy_punk
08-15-2005, 03:56 PM
Hey, I have this in a database:
<html>
<title>
$glob['sitename']
</title>
and I'm trying to get it to echo properly, so it'll parse the HTML as HTML and the variable will show it's value. Well, the problem I have is that I can do that, but not if it's coming from the database.


What happens is if I do:

$template = "
<html>
<title>
$glob['sitename']
</title>";
eval('$template = "$template";');
echo $template;

It parses it all fine. But if I get the template from teh database and run it through eval() it doesn't parse it properly.


Here's the whole function:

function get_template($part){
global $glob;
$query = mysql_query(" SELECT * FROM `$glob[prefix]-templates` WHERE `section`= '$part' ") or die(mysql_error());
$row = mysql_fetch_array($query);
$section = $row['section'];
$original = $row['original'];
$modified = $row['modified'];
if($row == FALSE){
die('Error getting template from database');
}
if($modified === ''){
$template = $original;
}else{
$template = $modified;
}
eval('$template = "$template";');
echo $template;
};

That will parse the HTML properly, but the title will be $glob['sitename'] - not the value of the variable. Which is really odd because if I just set $template to the stuff that's in the database, it works. It's just after I get it from the database that it doesn't.


Any help is appriciated :)

Burhan
08-16-2005, 03:29 AM
Did you try to print_r() or var_dump() what you are getting from the database?

mouldy_punk
08-16-2005, 07:48 AM
Yeah, I'm definately getting the right stuff from the database.

mouldy_punk
08-16-2005, 05:06 PM
ba-ba-bump-bump :\

gogocode
08-16-2005, 10:39 PM
Are you *sure* you're getting the right stuff, or more to the point, that you've inserted the right stuff in the database.

Check the contents of the table in your mysql client. Then echo $template before the eval and make sure it's what you think.

mouldy_punk
08-17-2005, 04:01 AM
It's definately the right stuff. Echoing $template does *nearlly* what I want it to, eg;


<title>$glob['sitename']</title>

will put $glob['sitename'] in the title bar of browsers - but it won't put the value of that variable. Even if I put <title><?php echo $glob['sitename']; ?> </title> it'll put "<?php echo...." in the title bar.

But don't worry about it now, I'm doing it another way because the way I was going to do it would kill the MySQL server ;p