【发布时间】:2016-11-10 21:09:35
【问题描述】:
我正在尝试在 Controller 类中运行原始 SQL 查询,但我无法弄清楚为什么我不能绑定多个参数。看看下面的sn-p:
$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." )
【问题讨论】: