php - 在 codeigniter 中获取不正确的查询

标签 php mysql codeigniter

当我使用上面的 从 mysql 表中获取记录时,我通过 Controller 将变量值作为 $id="'10','11',12'" 从 View 传递到模型>$id 值为:

$query=$this->db->query('select * from userdetails where Id IN ($ids)')

我没有得到结果,但它将查询打印为:

SELECT * FROM (`userdetails`) WHERE `Id` IN ('\'10\',\'11\',\'12\'')

如何解决这个问题?

最佳答案

好的试试这个:

$this->db->escape($id);
$query=$this->db->query("select * from userdetails where Id IN ($ids)");

在传入查询之前使用stripslashes($id);。或者 mysql_real_escape_string() 应该也可以。
这样:

$ids = stripslashes($ids);
$query=$this->db->query("select * from userdetails where Id IN ($ids)");

或者您可以发送以逗号分隔的值,以这种方式 $ids = "11,12,13"; 它应该有效。

关于php - 在 codeigniter 中获取不正确的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13989717/

相关文章:

php - Magento 管理员登录被重定向

php - 如何插入带有单引号 ' 符号的字符串

php - 如何根据 PHP 中输​​入的天数(例如 230、20 或 360 或任何天数)获取过去的日期(例如 2013-06-02)?

mysql - 为什么 MySQL 使用错误的索引?

Javascript 函数未在 Angular App 中返回值

php - 当我尝试将 magento 网站从服务器迁移到本地主机时出现错误

mysql - 按 Eloquent 最佳匹配排序

代码点火器 : Commands out of sync; you can't run this command now

php - 在 mySQL 数据库中存储时间范围并与当前时间进行比较

php - 外键为空时如何从连接中拉出行