ID from insert with multiple rows
Is it safe to assume that the auto increment id from a insert with multiple rows will be incremented by one starting to the mysql_last_insert_id()
value? Could there be a concurrent insert (from an other user session) between this multiple insert?
In this example tbl_name
has an autoincrement field called id
:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
$nbAffectedRows = mysql_affected_rows();
$idFirstRowInserted = mysql_last_insert_id();
$arrayId = array();
if($nbAffectedRows &a开发者_JS百科mp;gt; 0){
for($i = 0; $i <= $nbAffectedRows; $i++){
$arrayId[] = $idFirstRowInserted + $i;
}
}
Will $arrayId
always contain values incremented by one correponding to the 'id' of the rows inserted ?
It will be safe to assume that, if you wrap your INSERT
s in a transaction. As a side effect your query will run faster too.
精彩评论