1

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');
1
  • 1
    please show me the $id 's value. Commented Aug 3, 2017 at 10:13

5 Answers 5

7

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');
Sign up to request clarification or add additional context in comments.

5 Comments

Please remove extra -> in $this->db->->where_not_in('ID', $ids);
I tried but it deleted whole record. Please check my mysql query.
So what you want to try to delete?
Thanks. I got my mistake. Issue was, i was having string.
why it's get downvoted ? @Downvoter can you please explain .so that i can correct my mistake
1

You can write this in single line if you want to like this:

$this->db->delete('table name',array('field1'=>'value1','field2 !='=>'value2'));

short and simple.

Comments

0

Try this

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

This will join two arguments with AND clause


And Data should be

array

$ids = array(10, 20, '30);

Id

$style_id = 15;

Read Looking for Specific Data in codeigniter.com

3 Comments

$this->db->where('style_id', $style_id)->where_not_in('ID', $ids); this will bid dta with OR clause
I tried but it deleted whole record. Please check my mysql query.
what are you trying to do? explain please with respective data
0

You can run this directly :

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)
in $this->db->query("DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)");

Comments

0

// Try it

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

2 Comments

I tried but it deleted whole record. Please check my mysql query.
check again @BhuneshSatpada

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.