我有以下代码:
以上代码的输出是:
<?php
header('Content-Type: text/plain');
$db = new mysqli('localhost', 'username', 'password', 'my_schema');
$db->autocommit(false); // start transaction
$sql = "INSERT INTO my_table (col1, col2) VALUES (1, 2)";
$db->query($sql);
// $last_id = $db->insert_id;
// echo 'Last ID before commit: ' . $last_id . PHP_EOL;
$db->commit();
$db->autocommit(true); // end transaction
$last_id = $db->insert_id;
echo 'Last ID after commit: ' . $last_id . PHP_EOL;
$db->close();
?>
以上代码的输出是:
Last ID before commit: 1 <-- if uncomment the 2 lines above, it will show this
Last ID after commit: 0
insert_id
在autocommit
之外无法工作(返回0),即使它已经被正确提交和插入到数据库中。这是预期的行为吗?