php.net |  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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hans at nyphp dot org
New email:
PHP Version: OS:

 

 [2003-08-13 08:21 UTC] hans at nyphp dot org
Description:
------------
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:

http://lists.mysql.com/list.php?3:mss:9598:200308:ikdgphkkkddmffiinjik

Thanks,

Hans


Patches

Pull Requests

History

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

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

// output
Column count doesn't match value count at row 1
int(0)

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 ?

Mehdi
 [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`)
) TYPE=MyISAM;

The code:

<?php
$MYDB = mysql_connect('localhost','xxx','xxx');


$links = array('http://hans.zaunere.com',
               'http://zaunere.com',
               'http://hans.zaunere.com',
               'http://nyphp.org',
               'http://lists.nyphp.org',
               'http://nyphp.org'
              );


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:

http://lists.mysql.com/list.php?3:mss:9598:200308:ikdgphkkkddmffiinjik
 [2004-04-08 01:35 UTC] irchtml@php.net
I used the exact code you posted and mysql_insert_id() as documented (on php.net), 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 php.net docs).  Running the same script again (with the same data), the id's are 0, as advertised.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC