Skip to content

Commit 25dc931

Browse files
mattnikic
authored andcommitted
Fixed bug #80908
The last insert ID should be an unsigned integer. Closes GH-6810.
1 parent ec8de47 commit 25dc931

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ PHP NEWS
8888
. Fixed bug #40913 (PDO_MYSQL: PDO::PARAM_LOB does not bind to a stream for
8989
fetching a BLOB). (Nikita)
9090

91+
. PDO MySQL:
92+
. Fixed bug#80908 (PDO::lastInsertId() return wrong). (matt)
93+
9194
. PDO SQLite:
9295
. Fixed bug #38334 (Proper data-type support for PDO_SQLITE). (Nikita)
9396

ext/pdo_mysql/mysql_driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static zend_string *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const zend_string *
289289
{
290290
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
291291
PDO_DBG_ENTER("pdo_mysql_last_insert_id");
292-
PDO_DBG_RETURN(zend_i64_to_str(mysql_insert_id(H->server)));
292+
PDO_DBG_RETURN(zend_u64_to_str(mysql_insert_id(H->server)));
293293
}
294294
/* }}} */
295295

ext/pdo_mysql/tests/bug80908.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
Bug #80908: pdo_mysql lastInsertId() return wrong, when table id bigger than the maximum value of int64
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) die('skip not loaded');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
7+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8+
MySQLPDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
13+
14+
function createDB(): PDO {
15+
$db = MySQLPDOTest::factory();
16+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
17+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
18+
return $db;
19+
}
20+
21+
$db = createDB();
22+
$db->exec('DROP TABLE IF EXISTS test');
23+
$db->exec('CREATE TABLE test (`id` bigint(20) unsigned AUTO_INCREMENT, `name` varchar(5), primary key (`id`)) ENGINE = InnoDB AUTO_INCREMENT=10376293541461622799');
24+
25+
function testLastInsertId(PDO $db) {
26+
echo "Running test lastInsertId\n";
27+
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
28+
try {
29+
$db->exec("insert into test (`name`) values ('bar')");
30+
$id = $db->lastInsertId();
31+
echo "Last insert id is " . $id . "\n";
32+
} catch (PDOException $e) {
33+
echo $e->getMessage()."\n";
34+
}
35+
}
36+
37+
testLastInsertId($db);
38+
unset($db);
39+
echo "\n";
40+
41+
?>
42+
--CLEAN--
43+
<?php
44+
require __DIR__ . '/mysql_pdo_test.inc';
45+
MySQLPDOTest::dropTestTable();
46+
?>
47+
--EXPECT--
48+
Running test lastInsertId
49+
Last insert id is 10376293541461622799

0 commit comments

Comments
 (0)