【问题标题】:Delete Mysql record using array使用数组删除Mysql记录
【发布时间】:2012-03-07 20:10:01
【问题描述】:

我正在尝试使用 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是否可以将数组作为删除方法中的一个选项。

据此。 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 进行循环,但在我看来还有更好的方法。我想知道是否有人可以帮助我。非常感谢。

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    不太熟悉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();
    

    【讨论】:

    • 您可以链接您的操作:$this->db->where('IN ('.implode(',',$idsToDelete).')', NULL, FALSE)->delete( )
    【解决方案2】:

    真正接受数组作为参数的方法?

    我认为你的源代码应该是这样的:

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

    【讨论】:

      【解决方案3】:

      这篇文章已过时,但我认为仍然值得最佳/正确答案:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        • 2021-02-05
        • 1970-01-01
        相关资源
        最近更新 更多