php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54190 mysqli->insert_id will return zero after mysqli->get_warnings()
Submitted: 2011-03-08 12:16 UTC Modified: 2011-03-17 14:10 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: srwang at pixnet dot tw Assigned: mysql (profile)
Status: Wont fix Package: MySQLi related
PHP Version: 5.3.5 OS: FreeBSD 7.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: srwang at pixnet dot tw
New email:
PHP Version: OS:

 

 [2011-03-08 12:16 UTC] srwang at pixnet dot tw
Description:
------------
mysqli->insert_id will return zero after mysqli->get_warnings()
(In PHP 5.3.3, it is ok)

Test script:
---------------
$link = new mysqli('localhost', '', '', 'test');

$link->query("DROP TABLE `abcde`");
$link->query("CREATE TABLE abcde (`id` int(11) NOT NULL AUTO_INCREMENT, `value` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`));");
$link->query("INSERT INTO abcde SET `id` = 0");

print_r($link->get_warnings());
echo $link->insert_id . "\n";
echo $link->query("SELECT LAST_INSERT_ID() AS `id`")->fetch_object()->id . "\n";

Expected result:
----------------
mysqli_warning Object
(
    [message] => Field 'value' doesn't have a default value
    [sqlstate] => HY000
    [errno] => 1364
)
1
1

Actual result:
--------------
mysqli_warning Object
(
    [message] => Field 'value' doesn't have a default value
    [sqlstate] => HY000
    [errno] => 1364
)
0
1


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-17 11:53 UTC] andrey@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: mysql
 [2011-03-17 11:53 UTC] andrey@php.net
Works with libmysql, fails with mysqlnd.
 [2011-03-17 14:10 UTC] andrey@php.net
-Status: Assigned +Status: Wont fix
 [2011-03-17 14:10 UTC] andrey@php.net
Seems like an issue in libmysql, mysqlnd behaves correctly. Here is an excerpt from the documentation of mysql_insert_id(), which is the underlying C call.

"mysql_insert_id() returns 0 if the previous statement does not use an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the statement that generates the value."

"The reason for the differences between LAST_INSERT_ID() and mysql_insert_id() is that LAST_INSERT_ID() is made easy to use in scripts while mysql_insert_id() tries to provide more exact information about what happens to the AUTO_INCREMENT column."

mysqli calls internally "SHOW WARNINGS" to fetch the warnings from the server. The SHOW statement should reset insert_id in libmysql, but it does not. There is already reported bug about this for libmysql.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Apr 28 08:01:28 2025 UTC