duanfaxin7014 2011-10-15 01:42
浏览 66
已采纳

在PHP中调用数据库中的函数

I have this requirement that states that I must have a function signature as a value in a database column. So what I need to do is get that value that will then call the function in code. For example....let's say I have a column in a database named "Signature" that has a value of SendMail();. I need to be able to get the return value and then store it in another table. The problem is when I use the eval function on $i['Signature'] the function gets called, but I can't get the return value. What am I doing wrong? Here's an example:

$query = "SELECT signature FROM tbl WHERE id = 1 ";
$signature = mysql_query($query);
while ($i = mysql_fetch_array($signature)){
   //Here the function is being called properly but I can't get the return value.
   $email= eval($i['Signature']);
   if(mysql_num_rows($i) == 0){
      //insert the returned value into another table.
      $insertString = "INSERT INTO tbl (email) VALUES ('$email')";
   }
}

function SendMail()
{
   $email= 'Have a great day'
   return $email;
}

Also I'm bound to get some "eval is evil" comments, so if there's a better way please advise.

  • 写回答

2条回答 默认 最新

  • duannaiying9662 2011-10-15 01:46
    关注

    from the docs

    eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. 
    

    Therefore you should add return:

    $email= eval('return ' . $i['Signature']);
    

    alternatively:

    eval('$email = '  . $i['Signature']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题