|  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
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: srwang at pixnet dot tw
New email:
PHP Version: OS:


 [2011-03-08 12:16 UTC] srwang at pixnet dot tw
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");

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

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-17 11:53 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: mysql
 [2011-03-17 11:53 UTC]
Works with libmysql, fails with mysqlnd.
 [2011-03-17 14:10 UTC]
-Status: Assigned +Status: Wont fix
 [2011-03-17 14:10 UTC]
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-2021 The PHP Group
All rights reserved.
Last updated: Mon Dec 06 15:03:34 2021 UTC