I have the following codes:
<?php
header('Content-Type: text/plain');
$db = new mysqli('localhost', 'username', 'password', 'my_schema');
$db->autocommit(false);
$sql = "INSERT INTO my_table (col1, col2) VALUES (1, 2)";
$db->query($sql);
$db->commit();
$db->autocommit(true);
$last_id = $db->insert_id;
echo 'Last ID after commit: ' . $last_id . PHP_EOL;
$db->close();
?>
The output of the above code:
Last ID before commit: 1 <
Last ID after commit: 0
insert_iddoes not work outside autocommit(returns 0), even if it is correctly committed and inserted into the database. Is behavior expected?
source
share