[php-src] Issue #8746: PDO (mysql) unexpected lastInsertId when primary key is negative number
| From: | jtuta | Date: | Fri, 10 Jun 2022 12:29:59 +0000 |
| Subject: | [php-src] Issue #8746: PDO (mysql) unexpected lastInsertId when primary key is negative number | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241708@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8746
Author: jtuta
### Description
The following code:
```php
<?php
$dbh = new PDO('mysql:host=mysql;dbname=database', 'user', 'pass');
$dbh->prepare("CREATE TABLE
Datatable (id int NOT NULL,
value varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE Datatable ADD PRIMARY KEY (id), ADD UNIQUE KEY id
(id);
ALTER TABLE Datatable MODIFY id int NOT NULL
AUTO_INCREMENT;")->execute();
$stmt = $dbh->prepare("insert into Datatable (value,
id) values (?, ?)");
$stmt->execute( array('test', -1));
var_dump(
$dbh->lastInsertId(),
$dbh->query("SELECT * FROM Datatable")->fetch()
);
```
Resulted in this output:
```
string(20) "18446744073709551615" array(4) { ["id"]=> int(-1) [0]=>
int(-1) ["value"]=> string(4) "test" [1]=> string(4) "test" }
```
But I expected this output instead:
```
string(20) "-1" array(4) { ["id"]=> int(-1) [0]=> int(-1)
["value"]=> string(4) "test" [1]=> string(4) "test" }
```
Example has been run in docker image:
- php:8.1-fpm
- mysql image (if relevant) mysql:8.0.27
I
### PHP Version
PHP 8.1.7
### Operating System
Ubuntu 22.04