在Codeigniter中,如果我創建了一個執行多次插入的SQL字符串,那麼如何獲取每個插入的ID?如何獲取codeigniter中批量插入的ID?
// Prepare the SQL
$sql = '';
$chunk = array(array(), array(), array()); // The elements are arrays
foreach($chunk as $arr){
// The first field is the primary key (INT NOT NULL auto_increment)
$sql .= "(NULL, {$arr[0]}, {$arr[1]}, {$arr[2]}, {$arr[3]}, {$arr[4]})";
if($arr!= $last) $sql .= ', ';
}
// Start inserting into the db
$this->db->trans_start();
$this->db->query('INSERT INTO my_table VALUES '.$sql);
// A few other queries go here which need the IDs of the previous insert
$this->db->trans_complete();
這也是我第一次使用交易。
你能分享你如何管理去分享插入值的ID嗎? – Searock 2015-07-31 09:56:29