CodeIgniter: INSERT multiple records without cycle

前端 未结 1 1239
广开言路
广开言路 2020-12-09 15:06

It\'s possible to use multiple INSERT records in CodeIgniter Active Record without for, foreach and etc. ?

My current code:

foreach($tags as $tag) {
         


        
相关标签:
1条回答
  • 2020-12-09 15:45

    Codeigniter active record has a function insert_batch i think that is what you need

    $data = array(
       array(
          'title' => 'My title' ,
          'name' => 'My Name' ,
          'date' => 'My date'
       ),
       array(
          'title' => 'Another title' ,
          'name' => 'Another Name' ,
          'date' => 'Another date'
       )
    );
    
    $this->db->insert_batch('mytable', $data); 
    
    // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
    

    Works for both Codeigniter 3.x and Codeigniter 2.2.6

    UPDATED LINKS

    insert_batch() for Codeigniter 3.x

    insert_batch() for Codeigniter 2.x

    0 讨论(0)
提交回复
热议问题