I am wondering how exactly does lastInsertId()
work. Atm, I am using it like this to get the id of the inserted row so I can use this id in other sections of the code. For example:
$stmt = $db->prepare('INSERT INTO image_table (image_name, image_size) VALUES (:image_name, :image_size)');
$stmt->execute(array(
'image_name' => $photoName,
'image_size' => $image_size / 1024,
));
$lastInsertId = $db->lastInsertId('yourIdColumn');
Okay, my question is:
1) Does this script get the lastInsertId
for the SQL insert that was done in that particular script?
2) What happens, for example say, 3 different users inserted data into the same table just a few nano seconds difference. Something like this:
this script -> Inserts to row id 1
Another user -> Inserts to row id 2
Another user -> Inserts to row id 3
In that case, would the lastInsertId()
return the value 1 since it was the last id for the row that was inserted by that script or will it return 3 since that was the last id by the time the script came to the line that executes lastInsertId()
function?