Edit report at http://bugs.php.net/bug.php?id=54190&edit=1
ID: 54190 Updated by: and...@php.net Reported by: srwang at pixnet dot tw Summary: mysqli->insert_id will return zero after mysqli->get_warnings() -Status: Assigned +Status: Wont fix Type: Bug Package: MySQLi related Operating System: FreeBSD 7.3 PHP Version: 5.3.5 Assigned To: mysql Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2011-03-17 11:53:57] and...@php.net Works with libmysql, fails with mysqlnd. ------------------------------------------------------------------------ [2011-03-08 12:16:42] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54190&edit=1