When i use this in codeigniter it only selects the last array index
$array = array("status !="开发者_Go百科 => "deleted", "status !=" => "concept");
$this->db->where($array);
this is the result:
SELECT * FROM (`table`) WHERE `status` != 'concept'
Anyone knows why or knows a better way?
Since you've already figured out the reason for this on your own (you're simply overriding the array key), you should use one of the following options:
You can either pass it all in as a string in the first argument:
$this->db->where("status != deleted AND status != concept")->get('table');
Or you can make 2 separate method calls:
$this->db->where("status !=", "deleted")
->where("status !=", "concept")
->get('table');
The first one is simpler, but the second one is safer.
The way you do it should work, alternatively you can try this:
$array = array("status !=" => "deleted", "status !=" => "concept");
foreach($array as $k=>$v){
$this->db->where($k,$v);
}
If the column is the same for all clauses, a better/cleaner choice than multiple where()
s would be the where_not_in()
method.
It does just what it says - creates a NOT IN (..)
clause
Sample usage:
$this->db->where_not_in('status', $bad_statuses);
精彩评论