PDA

View Full Version : eval() troubles.


Majoracle
12-08-2006, 10:05 PM
I've been trying to create a template system, and ran into a problem that seems like it'd be a simple fix, but I can't seem to find the answer. I'm low on time, so I'll just you the code I have and the error rather than explaining it.

Get Template Function (works):
function fetch_template($name)
{
$get_template = "SELECT `template` FROM `templates` WHERE `name`='$name' LIMIT 1";
$get_template_res = mysql_query($get_template);
$fetch_template = mysql_fetch_array($get_template_res);
$template = $fetch_template['template'];

return $template;
}

Eval Function and Echo Output:
eval('$display = "' . fetch_template('template_name') . '";');
echo $display."\n";

PHP Error:
Parse error: syntax error, unexpected '<' in path/to/php/file(24) : eval()'d code on line 3


I'm pretty sure it's a problem with the eval function reading HTML from the template. Just can't figure out a fix.


Thanks in advance

jjshell
12-08-2006, 10:25 PM
the evaled code has a syntax error on line 24.
that's it :)

Fumigator
12-08-2006, 10:50 PM
From your post it sounds like what gets returned from your fetch_template() function is HTML code. Well of course that's not going to work-- the eval() function requires PHP code. So unless the string is "print '<html blah blah>'" and there's just errors in fetch_template() (which you say there isn't), you'll need to figure out another way.

Majoracle
12-09-2006, 12:44 AM
It IS PHP. There is HTML in it, but it's in PHP format. The point is, no matter what it's evaluating, it still returns errors. I even limited down to extracting only this from the template:

$str = \"test\";
echo $str;

and what it returns is this:
http://www.katiefanatics.com/newversion_beta/testtemplate.php


Sorry if I sound a bit frustrated. I've spent years and countless hours trying to understand the eval function, but I've never gotten anywhere.

Andras
12-09-2006, 01:48 AM
If you are making an eval(); you cant forget you have to put backslashes before a variable you want to set:


eval("\$display='...';");