0

I am trying to delete multiple record(s) by using codeigniter

$this->db->delete() and $this->db->where()

I want to delete record(s) with an array like

$id =array(
  0=>'13',   //13 is the id
  1=>'4',    //4 is the id
  2=>'2'     //2 is the id
); 

The array was generated by the users so it will be dynamic. I just want to know if codeigniter can take the array as a option in delete method.

According to this. http://codeigniter.com/user_guide/database/active_record.html

array won't work in the following method.

$this->db->delete()
$this->db->where('id', $id); //I don't think I can do this. 

I could use foreach to loop but it seems to me there are better ways. I was wondering if anyone could help me about it. Thanks a lot.

3 Answers 3

3

Not really familiar with codeigniters active record but I believe the stmt you would like is:

$sql = "DELETE FROM tbl WHERE id IN (".implode(',',$idsToDelete.");";
$this->db->query($sql);

This might work better with active record:

$this->db->where('IN ('.implode(',',$idsToDelete).')', NULL, FALSE);
$this->db->delete();
Sign up to request clarification or add additional context in comments.

1 Comment

You can chain your actions: $this->db->where('IN ('.implode(',',$idsToDelete).')', NULL, FALSE)->delete()
1

The method where really accept an array as parameter?

I think your source code should look:

$this->db->delete()
$this->db->where($id['i_index']); 

Comments

0

This post is dated, but I figured still deserves the best/right answer:

    $this->where_in('id', array(13, 4, 2))->delete('db_table_name');

Comments

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.