INSERT IGNORE using Codeigniter

后端 未结 13 940
眼角桃花
眼角桃花 2020-12-08 19:52

I am trying to insert a few rows into the MySQL table using Codeigniter and Active Records.

PHP Code

$data = array(\'......\');  //          


        
13条回答
  •  温柔的废话
    2020-12-08 20:32

    As I also encountered a similar problem, I finally chose a little bit more "elegant" solution like the one below. A complete insert_batch query that is something that uses Rocket's answer AND transactions:

    $this->db->trans_start();
    foreach ($items as $item) {
           $insert_query = $this->db->insert_string('table_name', $item);
           $insert_query = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $insert_query);
           $this->db->query($insert_query);
        }
    $this->db->trans_complete();
    

    That will also wrap everything on a transaction resulting on a faster query like doing a insert_batch(). Well not as fast as insert_batch() but faster than a single query for each entry of course. I hope it will help someone.

提交回复
热议问题