我正在尝试使用 codeigniter 删除多条记录
$this->db->delete() and $this->db->where()
我想用这样的数组删除记录
$id =array(
0=>'13', //13 is the id
1=>'4', //4 is the id
2=>'2' //2 is the id
);
数组是由用户生成的,因此它将是动态的。 我只想知道 codeigniter 是否可以将数组作为 delete 方法中的一个选项。
据此。 http://codeigniter.com/user_guide/database/active_record.html
数组在以下方法中不起作用。
$this->db->delete()
$this->db->where('id', $id); //I don't think I can do this.
我可以使用 foreach 来循环,但在我看来还有更好的方法。我想知道是否有人可以帮助我。多谢。
最佳答案
不太熟悉 codeigniters 事件记录,但我相信您想要的 stmt 是:
$sql = "DELETE FROM tbl WHERE id IN (".implode(',',$idsToDelete.");";
$this->db->query($sql);
这可能更适用于事件记录:
$this->db->where('IN ('.implode(',',$idsToDelete).')', NULL, FALSE);
$this->db->delete();
关于mysql - 使用数组删除Mysql记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608335/