phpcodeigniterquery-builderwhere-inblacklist

CodeIgniter's where_not_in() with a dynamic blacklist string of values only works when a single-value string


I'm having an issue with CodeIgniter's where_not_in() method. I am trying to exclude a series of ID's passed in as a comma-delimited string.

I can't understand why everything worked fine and dandy with one record, but not with multiple.

My Query

$this->db->where_not_in('crm.user_id', $ignore);

The problem is when I profile the the Query is wrong.

With a string of ID's

// $ignore = "12,13";    
SELECT *
FROM (`crm`)
WHERE `crm`.`user_id` NOT IN ('16,13') 
AND `survey` =  1 

With a string of Quotes ID's

// $ignore = "'12','13'";
SELECT *
FROM (`crm`)
WHERE `crm`.`user_id` NOT IN ('\'16\',\'13\'') 
AND `survey` =  1 

Am I forced to do a loop of "or_where_not_in" or something like that?


Solution

  • where_in and where_not_in expect you to pass an array, not a string as the 2nd parameter.

    $ignore = array(12, 13);
    
    $this->db->where_not_in('crm.user_id', $ignore);
    

    Link to the docs: http://www.codeigniter.com/userguide2/database/active_record.html