pdo-database-class icon indicating copy to clipboard operation
pdo-database-class copied to clipboard

Get last insert ID for transaction

Open tmblog opened this issue 8 years ago • 1 comments

How can I get the last ID using a transaction?

$db->startTransaction(); ... if (!$db->insert('myTable', $insertData)) { //Error while saving, cancel new record $db->rollback(); } else { //OK $db->commit(); }

If I do: $id = $db->insert('myTable', $insertData) I get duplicate inserts

tmblog avatar Nov 29 '18 17:11 tmblog

Refactored with php 8.4+ support. Please, check the new version.

tommyknocker avatar Oct 10 '25 09:10 tommyknocker