我试图运行一个原始的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和:notIn)时,查询返回,但:notIn似乎在查询中没有效果。它看起来没有被绑定。
考虑这两个参数,我如何运行这个查询?
谢谢你的帮助
更新:绑定实际上正在工作,但它将:notIn
绑定为字符串,因此执行的查询是.... not in ('1,2,3')
我已经解决了我的问题,只是确保一切都是一个数字,只是在查询中连接:...u.id not in( ".$notInStr." )
phalcon正在使用的标准pdo库和phalcon db正在包装的pdo库不接受数组作为绑定参数。您需要使用phalcon模型
利用Phalcon'Mvc'Model'Query'Builder
。有一个inWhere
方法,它接受数组作为参数。
记住永远不要使用你从最终用户那里得到的未经过滤的数据,就像你刚刚做的那样。这使得您的应用程序容易受到sqlinject攻击。