Bug #76896 [NEW]: PDO::lastInsertId resets after SELECT

From: Date: Mon, 17 Sep 2018 14:43:24 +0000
Subject: Bug #76896 [NEW]: PDO::lastInsertId resets after SELECT
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217099@lists.php.net to get a copy of this message
From:             symos at yahoo dot com
Operating system: Ubuntu 18.04
PHP version:      7.2.10
Package:          PDO MySQL
Bug Type:         Bug
Bug description:PDO::lastInsertId resets after SELECT

Description:
------------
When executing an INSERT query and then immediately running
PDO::lastInsertId(), the value it returns is correct. But if a SELECT
query is executed in between, lastInsertId returns 0. This did not
happen in PHP 5.5.9.

If you run "SELECT LAST_INSERT_ID();" as a raw query (even through PDO)
the result you get is correct. 

I can't be sure if this is a bug or was changed intentionally, but I
would argue it's not correct for PDO's lastInsertId to return a
different value than MySQL's LAST_INSERT_ID().

Test script:
---------------
<?php

$username = "username";
$password = "password";    

$pdo = new PDO("mysql:host=127.0.0.1;port=3306;dbname=test_db",
$username, $password);
    
$pdo->query("CREATE TABLE IF NOT EXISTS test_table (
      id int(11) NOT NULL AUTO_INCREMENT,
      random_field int(11) NOT NULL,
      PRIMARY KEY (id)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;");

$st = $pdo->query("INSERT INTO test_table(random_field) VALUES (" .
rand(1, 100000) . ")");
    
if($st){
        echo "last_insert_id: " . $pdo->lastInsertId() . "\n";
        $pdo->query("SELECT 1 + 1");
        echo "last_insert_id: " . $pdo->lastInsertId() . "\n";
}
else {
        var_dump($pdo->errorInfo());
}

Expected result:
----------------
Both values should reflect ID of last inserted record

Actual result:
--------------
Second value is zero

-- 
Edit bug report at https://bugs.php.net/bug.php?id=76896&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=76896&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=76896&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=76896&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=76896&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=76896&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=76896&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=76896&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=76896&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=76896&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=76896&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=76896&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=76896&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=76896&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76896&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=76896&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=76896&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=76896&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76896&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=76896&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=76896&r=mysqlcfg



Thread (7 messages)

« previous php.bugs (#217099) next »