CI frame database bulk inserts insert_batch ()

Use of the CI AR operations: insert_batch () of database access can be reduced. A visit can be. Example 1:


$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); 

//生成: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another dat

e')

 1 示例2:
 2 
 3 $one_info = array();
 4 $insert_data = array();
 5 $one_info['role_id'] = 6;
 6 $one_info['operator'] = 'test';
 7 for($i = 0; $i <= 3; $i++) {
 8             $one_info['net_id'] = $i;
 9             $insert_data[] = $one_info;
10 }
11 if (!$this-> db-> insert_batch (on tableA, $ insert_data )) {
 12 is        return . 3 ;  
 13 is  }
 14  
15  // insert sql statement is insert into tableA (role_id, operator, net_id) values (6, 'test', 0), (6, 'test', 1 ), (6, 'test', 2);

NOTE: The first parameter contains a table name, the second is an associative array containing data.

  

Guess you like

Origin www.cnblogs.com/zb1690194137/p/11038559.html