How to find last inserted id while using pdo transaction?

谁都会走 提交于 2019-12-11 14:54:43

问题


I'm using PDO in my web application. In a part of this application, I need to work with PDO Transactions. I need to know last inserted id of first query and use it in the second query, and then if no problem occurs, I will commit this transaction.
The problem I have is that how can I find out last inserted id before transaction commit?
This is a sample of my need :

$db->beginTransaction();  

$stmt1 = "INSERT ...";  // An insert query
$q = $db->prepare($stmt1);  
$q->execute(array());  

$last = $db->lastInsertId();

$stmt2 = "UPDATE ...";  // An update query
$q2 = $db->prepare($stmt2);  
$q2->execute(array($last));  

$db->commit();

来源:https://stackoverflow.com/questions/18133431/how-to-find-last-inserted-id-while-using-pdo-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!