codeigniter和在其中使用多索引(数组)函数

时间:2011-09-07 01:50:23

标签: php mysql codeigniter

当我在codeigniter中使用它时,它只选择最后一个数组索引

$array = array("status !=" => "deleted", "status !=" => "concept");
$this->db->where($array);

这是结果:

SELECT * FROM (`table`) WHERE `status` != 'concept'

任何人都知道为什么或知道更好的方法?

3 个答案:

答案 0 :(得分:2)

由于您已经自己想出了这个原因(您只是覆盖了数组键),因此您应该使用以下选项之一:

您可以在第一个参数中将其全部作为字符串传递:

$this->db->where("status != deleted AND status != concept")->get('table');

或者您可以进行两次单独的方法调用:

$this->db->where("status !=", "deleted")
         ->where("status !=", "concept")
         ->get('table');

第一个更简单,但第二个更安全。

答案 1 :(得分:0)

你应该这样做,或者你可以试试这个:

$array = array("status !=" => "deleted", "status !=" => "concept");
foreach($array as $k=>$v){
    $this->db->where($k,$v);
}

答案 2 :(得分:0)

如果所有子句的列相同,则where_not_in()方法的选择比多个where()更好/更清晰。 它正如它所说的那样 - 创建一个NOT IN (..)子句

样本用法:

$this->db->where_not_in('status', $bad_statuses);