#46285 [Opn->Asn]: lastInsertId() returns "0" when a deferenced PDOStatement is executed

From: Date: Mon, 13 Oct 2008 22:41:23 +0000
Subject: #46285 [Opn->Asn]: lastInsertId() returns "0" when a deferenced PDOStatement is executed
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-129674@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: Open +Status: Assigned Bug Type: PDO related Operating System: Linux PHP Version: 5.3CVS-2008-10-13 (CVS) -Assigned To: +Assigned To: johannes 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

« previous php.bugs (#129674) next »