php - 如何从数据库列中检查 `if` 语句,如 '$a > $b'

标签 php mysql codeigniter

如何查看if来自数据库列的语句,如 $a > $b$a == $b$a != $b$a < $b

我从 $row['condition'] 中的数据库中获取数据 $row['condition'] = '$a == $b'; 或者 $row['条件'] = '100 == 10';

在 if 语句中我想检查

if($row['condition']){
   echo 'true';
}else{
   echo  'false';
}

我要输出

false

最佳答案

这样做的一种方法是使用 eval()功能。但正如手册所述,这非常危险:

Caution:
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.1

也就是说,可以使用这段代码:

$row['condition'] = '$a == $b';
$a=10;
$b=20;

// Evaluate the condition using "return your_condition;"
if (eval( 'return ' . $row['condition'] . ';' ))
{
    echo "true";
}
else
{
    echo "false";
}

测试一下 here .


1 http://php.net/manual/en/function.eval.php

关于php - 如何从数据库列中检查 `if` 语句,如 '$a > $b',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637376/

相关文章:

php - 如何在mysql表中将默认值设置为文本并自动递增

php - REGEX 用于清理具有多个空格、制表符、空行的多行字符串 (PHP Preg_Replace)

php - 使用命名占位符设置 PDO/MySQL LIMIT

php - 将排序算法应用于数据库查询

Mysql Entity Framework 问题 - 指定的键太长;最大 key 长度为 3072 字节

javascript - jQuery 自动完成 ajax 请求不显示返回的数据

javascript - 按钮需要点击两次才能工作,我希望只点击一次

Codeigniter:来自数据库表的/application/config/constant.php中的常量

php - 从 Ratchet websocket 向客户端发送消息

php - SQL Server 的 mysql_real_escape_string 替代方案