mysql - 如果数组为空,where_in codeigniter 错误

标签 mysql activerecord codeigniter-2

我正在使用 Codeigniter 2。我正在尝试使用 Active Record 进行 mysql 查询。问题是当传递给 where_in 的数组为空时出现错误。我问如果数组为空,是否有任何方法可以忽略 where_in 条件

这是条件示例:

$this->db->where_in('p.brand_id', $brands);

这是我得到的错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND p.brand_id IN ()

最佳答案

如果数组为空,则不要调用 where_in()

if(is_array($brands) && count($brands) > 0){
    $this->db->where_in('p.brand_id', $brands);
}

关于mysql - 如果数组为空,where_in codeigniter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22356372/

相关文章:

php - 无法将文件 CSV 上传到托管数据库

php - Facebook 登录困惑

php - 在 Laravel 中的关系表中添加 where 子句

ruby-on-rails - 如何在模型上引用 'user_id' ,但命名为两个不同的属性 'poster_id' 和 'receiver_id' ? - rails 3

php - foreach 循环突出显示数据库中的单元格

ruby-on-rails - Rails 计算在哪里

ruby-on-rails - Rails where() 和时区

php - 在库文件codeigniter中加载不同的数据库

mysql - SQL:使用 UNION 或子查询后无法使用 UPDATE 子句,而类似的 SELECT 仍然有效

mysql - 1 :n relationship and sql query