Search code examples
phpmysqlcodeigniteractiverecordquery-builder

CodeIgniter Delete where condition and where not in given array


How to run delete query in Codeigniter. I have set of array and a key. In MySQL my query run perfectly.

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)

In Codeigniter how to write and run this query. I tried below code but not working.

$ids = '5,9,12';
$this->db->where('style_id', $style_id)->where_not_in('ID', $ids);
$this->db->delete('TABLE');

Solution

  • Try this: you must have $ids as array

    $ids = array(5,9,12);
    $style_id= 2;
    
    
    $this->db->where('style_id', $style_id);
    $this->db->where_not_in('ID', $ids);
    $this->db->delete('TABLE');