Php 从数据库加载时出现评估错误
这是我的密码Php 从数据库加载时出现评估错误,php,mysql,eval,Php,Mysql,Eval,这是我的密码 $script = $row['script']; // $row is from database and the $row['script'] is exeactly the same with $script commented below. // $script ="global \$timedate;\$target =\$timedate->now();return \$target;"; return eval($script); 当我取消注释
$script = $row['script'];
// $row is from database and the $row['script'] is exeactly the same with $script commented below.
// $script ="global \$timedate;\$target =\$timedate->now();return \$target;";
return eval($script);
当我取消注释$script时,它将正确运行。但是,如果我输入$script并从$row['script']加载值,则eval get错误如下:
[12-May-2012 22:09:25 UTC] PHP Parse error: syntax error, unexpected $end in C:\Users\iopen\Documents\IOPEN\Project\Clients\sugarcrm\hfcrmlocal\custom\modules\iopenwf\model\IOpenAction.php(56) : eval()'d code on line 3
有什么想法吗?不要使用
eval
这是个非常糟糕的主意
出于实验目的,您可以使用stripslashes
删除大多数斜杠问题
class Test {
function now() {
return time ();
}
}
$timedate = new Test ();
$script = "global \$timedate;\$target =\$timedate->now();return \$target;";
$script = stripslashes ( $script );
var_dump ( eval ( $script ) );
试着为你的脚本做一个函数,这样以后编辑起来会更容易,你会有一个更好的概览。 正如您可能看到的,我删除了eval();功能。现在它完成了任务。我不确定您为什么需要eval(),但在下面的脚本中,没有eval();需要
$script变量的内容是什么?$script=“global\$timedate;\$target=\$timedate->now();return\$target;”它不能完全相同。可能db在$
符号之前包含一个文字反斜杠,而在PHP字符串上,反斜杠是转义字符?eval()'d代码,位于第3行。。。我在上面提到的注释中没有看到任何第3行。只需删除文本反斜杠。我尝试了如下代码:$script=$row['script']$脚本=斜杠($script);返回变量转储(eval($script));我得到了错误:[12-May-2012 22:45:39 UTC]PHP解析错误:语法错误,在C:\Users\iopen\Documents\iopen\Project\Clients\sugarcrm\hfcrmlocal\custom\modules\iopenwf\model\IOPSecution.PHP(54):eval()中出现意外的“&”我确信新错误与您调用的类有关。。。你能不能让我自己的类也把数据库的内容放进pastebin让我们看看..如果我运行下面的代码,它会成功:$script=“global\$timedate;\$target=\$timedate->now();return\$target;”;返回eval($script);但是$script=$row['script']失败;返回eval($script);全球$时间日期;来自sugarcrm。这个应该没问题,因为我经常在其他地方使用它。对不起,它对数据库中的“return\$target='High';”都不起作用。我将在今晚晚些时候继续。如果我运行下面的代码,它将成功:$script=“global\$timedate;\$target=\$timedate->now();return\$target;”;返回eval($script);但是$script=$row['script']失败;返回eval($script);
<?php
function getScript() {
$script = 1;
// $row is from database and the $row['script'] is exeactly the same with $script commented below.
// $script ="global \$timedate;\$target =\$timedate->now();return \$target;";
return $script;
}
$scriptNo = getScript();
echo $scriptNo;
?>