mysql - 使用数组删除Mysql记录

标签 mysql codeigniter

我正在尝试使用 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/

相关文章:

PHP错误 undefined variable : result1 help!

mysql - 如何使用 update() 在 Django 的日期时间字段上增加年份?

javascript - 我们如何根据选择选项发送电子邮件至

php - Codeigniter:何时使用模型与库?

php - 如何从 Web 浏览器隐藏 SQL 查询 (PHP)

mysql - 从另一台服务器恢复MySQL数据库,会影响当前数据库吗?

php - 与用户一起发送我自己的 $_POST 数据

CodeIgniter:图像处理类不能工作两次

php - 在 Codeigniter 上创建权限而不检查 URI 段

php - CodeIgniter force_download() 问题