From: phpwnd at gmail dot com
Operating system: Linux
PHP version: 5.3CVS-2008-10-13 (CVS)
PHP Bug Type: PDO related
Bug description: lastInsertId() returns "0" when PDOStatement is executed by
dereference
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 bug report at http://bugs.php.net/?id=46285&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=46285&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=46285&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=46285&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=46285&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=46285&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=46285&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=46285&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=46285&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=46285&r=support
Expected behavior: http://bugs.php.net/fix.php?id=46285&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=46285&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=46285&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=46285&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=46285&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=46285&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=46285&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=46285&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=46285&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=46285&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=46285&r=mysqlcfg