php / php/doc-en

PDO::lastInsertId returns 18446744073709551615 instead of -1

Open
#1,930 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: pdo
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the result with the provided max_id table and PHP example, then inspect php-src/ext/pdo_mysql/mysql_driver.c around line 296 and the referenced zend_u64_to_str behavior. Confirm whether this doc-en issue requires a documentation update or belongs in php-src, and document the verified expected behavior and reproduction details.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.