如何执行存储在MySQL数据库中的PHP函数

时间:2014-04-13 08:06:48

标签: php mysql function

我希望执行一个存储在mysql表中的php函数。 e.g

SQL query: SELECT * FROM `modules` WHERE uid=2 LIMIT 0, 30 ;

Rows: 1

uid     moduleName  source  responsiveCode  active  
2   Testimonial     this is a function <?php echo getUrl(); ?>  12  0

现在我想在每次检索uid编号2时执行getUrl()函数

1 个答案:

答案 0 :(得分:0)

您可以像这样使用eval()

$result = mysqli_query($connection, "SELECT * FROM `modules` WHERE uid=2 LIMIT 0, 30");
$row = mysqli_fetch_assoc($result);
$function = $row['source'];

eval('?>' . $function . '<?php');

我强烈反对您将功能存储在数据库中,因为它会带来巨大的安全风险。相反,你可以这样做:

uid | moduleName  | source             | responsiveCode | active 
---------------------------------------------------------------- 
2   | Testimonial | this is a function | 12             | 0

$result = mysqli_query($connection, "SELECT * FROM `modules` WHERE uid=2 LIMIT 0, 30");
$row = mysqli_fetch_assoc($result);
echo $row['source'] . getUrl(); //will still print the same