Skip to content

PDO::lastInsertId returns 18446744073709551615 instead of -1 #1930

@mowangjuanzi

Description

@mowangjuanzi

Description

SQL to create max_id table:

CREATE TABLE `max_id` (
  `id` int NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2147483647 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

The following code:

<?php
$pdo = new PDO("mysql:dbname=test;host=127.0.0.1", 'root', 'password');
$stmt = $pdo->prepare("INSERT INTO max_id (id, title) VALUES(?, ?)");
var_dump($stmt->execute([-1, "C"]));

Resulted in this output:

"18446744073709551615"

But I expected this output instead:

"-1"

I found that the problem may be in https://github.com/php/php-src/blob/master/ext/pdo_mysql/mysql_driver.c#L296

zend_u64_to_str does not recognize negative IDs.

PHP Version

8.1.11

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions