Most of time we need to insert multiple rows at a time into the database but some developer use multiple INSERT statements to insert multiple rows while it is a bad approach to solve this problem. Codeigniter provides a insert batch function.
Example:-
$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);
or
$sql = array();
foreach( $data as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['text']).'", '.$row['category_id'].')';
}
$this->db->query('INSERT INTO table (text, category) VALUES '.implode(',', $sql));
0 Comment(s)