#46285 [Asn->Csd]: lastInsertId() returns "0" when a deferenced PDOStatement is executed
| From: | johannes@php.net | Date: | Mon, 27 Oct 2008 17:29:30 +0000 |
| Subject: | #46285 [Asn->Csd]: lastInsertId() returns "0" when a deferenced PDOStatement is executed | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-130220@lists.php.net to get a copy of this message | ||
ID: 46285
Updated by: johannes@php.net
Reported By: phpwnd at gmail dot com
-Status: Assigned
+Status: Closed
Bug Type: PDO related
Operating System: Linux
PHP Version: 5.3CVS-2008-10-13 (CVS)
Assigned To: johannes
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2008-10-13 18:11:31] phpwnd at gmail dot com
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.
------------------------------------------------------------------------
[2008-10-13 18:04:55] 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"
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46285&edit=1