|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #25075 mysql_insert_id return value
Submitted: 2003-08-13 08:21 UTC Modified: 2004-04-08 01:35 UTC
From: hans at nyphp dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: All
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: hans at nyphp dot org
New email:
PHP Version: OS:


 [2003-08-13 08:21 UTC] hans at nyphp dot org
It's recently come to my attention that the documentation for the mysql_insert_id wasn't indicating the correct return behavior of the function.  I double checked this with a small C program, and found the results to be the same.  Finally, after raising this issue with the MySQL folks, they indicated this was a documentation error.  The correct behavior is indicated at:




Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-14 04:12 UTC]
I just had a test :

// code
mysql_query('insert into foo values (1)'); // foo have 2 fields, first one is an auto_increment

// output
Column count doesn't match value count at row 1

From the documentation : "mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value."

The documentation seems okay. bogus bug ?

 [2003-08-14 06:06 UTC] hans at nyphp dot org
That code doesn't reproduce the bug.  This does:

The table:  

CREATE TABLE `links` (
  `linkid` int(10) unsigned NOT NULL auto_increment,
  `link` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`linkid`),
  UNIQUE KEY `link` (`link`)

The code:

$MYDB = mysql_connect('localhost','xxx','xxx');

$links = array('',

foreach( $links as $key => $link ) {

   $tmp = mysql_escape_string($link);

   mysql_query("INSERT INTO xxx.links (linkid,link)
                VALUES (NULL,'$tmp')", $MYDB);

   $R_linkid = mysql_insert_id($MYDB);

   echo "<pre>Array key: $key <br>Link: $link <br>Linkid: $R_linkid </pre><br><br>";

This is not a bogus bug; MySQL has identified it as a documentation bug, as I pointed to with:
 [2004-04-08 01:35 UTC]
I used the exact code you posted and mysql_insert_id() as documented (on, returning the id 5 for the last insert as the last_insert_id is not reset unless a new auto_increment value is created (as said in the docs).  Running the same script again (with the same data), the id's are 0, as advertised.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 17:03:35 2021 UTC