我试图在一个Controller类中运行一个原始的SQL查询,但是我很难弄清楚为什么我不能绑定多个参数。看一下下面的代码片段:
$sql = "select
u.id,
u.name,
u.email,
r.name as role
from user u
inner join role r on r.id = u.role_id
left join user_group_user ugu on ugu.user_id = u.id and ugu.user_group_id = ".$user_group_id."
where (u.name like :search or u.email like :search or r.name like :search)
and ugu.user_id is null and u.id not in( :notIn )
order by u.name, r.name";
$users = $this->db->query($sql, ['search' => '%'.$search.'%', 'notIn'=>$notInStr ])->fetchAll();$notIn变量的值类似于1,2,3。如果我只用参数:search做同样的事情,那么查询就能正常工作。当我尝试使用两个参数(:search和:notIn)时,查询将返回,但:notIn似乎对查询没有影响。它看起来没有被绑定。
如何在考虑到这两个参数的情况下运行此查询?
谢谢你的帮助
更新:绑定实际上是有效的,但是它将:notIn绑定为字符串,所以执行的查询是.... not in ('1,2,3')
我已经解决了我的问题,只要确保所有东西都是一个数字,并在查询中连接:...u.id not in( ".$notInStr." )
发布于 2016-11-11 05:14:41
phalcon正在使用的标准pdo库和phalcon db正在包装的标准pdo库不接受数组作为绑定参数。您需要使用phalcon模型。
发布于 2016-11-17 07:21:14
使用Phalcon\Mvc\Model\Query\Builder。有一个inWhere方法,它接受数组作为参数。
记住,永远不要像刚才那样使用从最终用户那里获得的未经过过滤的数据。这使得您的应用程序容易受到SQLInjection攻击。
https://stackoverflow.com/questions/40537040
复制相似问题