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条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)