我想知道究竟是如何lastInsertId()
工作的。Atm,我正在使用它来获取插入行的 id,这样我就可以在代码的其他部分使用这个 id。例如:
$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');
好的,我的问题是:
1)该脚本是否获取lastInsertId
在该特定脚本中完成的 SQL 插入?
2)会发生什么,例如,3 个不同的用户将数据插入同一个表中,只是相差几纳秒。像这样的东西:
this script -> Inserts to row id 1
Another user -> Inserts to row id 2
Another user -> Inserts to row id 3
在这种情况下,返回值 1 是因为它是该脚本插入的行的最后一个 id,还是返回 3,因为这是脚本到达执行函数lastInsertId()
的行时的最后一个 id ?lastInsertId()