php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50580 mysql_insert_id not returning last inserted ID
Submitted: 2009-12-26 17:28 UTC Modified: 2009-12-26 18:10 UTC
From: michaelozeryansky at hotmail dot com Assigned:
Status: Closed Package: MySQL related
PHP Version: 5.3.1 OS: Windows XP Pro SP 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michaelozeryansky at hotmail dot com
New email:
PHP Version: OS:

 

 [2009-12-26 17:28 UTC] michaelozeryansky at hotmail dot com
Description:
------------
I have never used mysql_insert_id() before, but I think its not too hard to get right. From what I know it will return "The ID generated for an AUTO_INCREMENT column by the previous query on success" as stated on php.net/manual.

When I run my insert query, which inserts multiple(10) rows, the mysql_insert_id only gives the 'first' id used and not the last id. In my code this is actually more useful to me, but I had to figure it out the hard way.

Is this a bug or a feature?

P.S. I'm using 5.3.0, but I checked and 5.3.1 doesn't mention this

Reproduce code:
---------------
$query = 'INSERT INTO player VALUES ';
foreach($data['players'] as $player){
	$query .= sprintf("(null, '%s', '%s', '%s', '%s', '%s', %s),", $stuff, $stuff, $stuff, $stuff, $stuff, $stuff);		
}
$query = substr($query,0,-1);//removes last comma
$result = mysql_query($query, $resource) or die(mysql_error());
$firstPlayerID = mysql_insert_id($resource);

Expected result:
----------------
10 players, starting at AUTO=1
result should be = 10

Actual result:
--------------
10 players, starting at AUTO=1
result is = 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-26 18:08 UTC] rasmus@php.net
Your argument is sound, but there isn't much we can do here.  The 
mysql_insert_id() function in PHP just calls the MySQL client library 
function of the same name.  We don't do anything at the PHP level here, 
we simply give you what the Database and the client library pass back 
to us.  So, to pursue this you need to ask the MySQL folks.
 [2009-12-26 18:10 UTC] michaelozeryansky at hotmail dot com
Alright thanks, I wanted to ask PHP first just in case. 
I'm off to MySQL.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 10:01:32 2024 UTC