|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-10-13 18:04 UTC] phpwnd at gmail dot com
Description:
------------
lastInsertId() always returns "0" after executing a dereferenced native (non-emulated) prepared statement. Storing the PDOStatement in a variable _then_ executing it solves that problem, and so does using emulated prepared statements.
PHP was compiled with --enable-debug --with-pdo-mysql=mysqlnd, I haven't tested with other combinations yet.
Reproduce code:
---------------
<?php
$db = new PDO('mysql:dbname=test;unix_socket=/var/run/mysqld/mysqld.sock', 'user', 'password', array(
PDO::ATTR_EMULATE_PREPARES => false
));
$db->exec('DROP TABLE IF EXISTS test');
$db->exec('CREATE TABLE test (id INT auto_increment, PRIMARY KEY (id))');
$sql = 'INSERT INTO test (id) VALUES (NULL)';
$db->prepare($sql)->execute();
var_dump($db->lastInsertId());
$stmt = $db->prepare($sql);
$stmt->execute();
var_dump($db->lastInsertId());
?>
Expected result:
----------------
string(1) "1"
string(1) "2"
Actual result:
--------------
string(1) "0"
string(1) "2"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 17 14:00:02 2025 UTC |
I had just submitted this bug when I realized I didn't have to recompile PHP to test another database library. The same reproduce code with $db = new PDO('sqlite::memory:'); ...shows the expected result, so I assume the bug can be attributed to mysqlnd or possibly PDO_MYSQL.