php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69714 insert_id does not return id
Submitted: 2015-05-27 21:08 UTC Modified: 2015-05-29 01:39 UTC
From: markem at sim1 dot us Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.5.25 OS: Windows 7
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: markem at sim1 dot us
New email:
PHP Version: OS:

 

 [2015-05-27 21:08 UTC] markem at sim1 dot us
Description:
------------
Situation:  Inserting records in to a table.  The record ID number is being inserted into the table.  When this is done AND the record has been inserted THEN a call to the insert_id() function in either way (ie: $db->insert_id or mysqli_insert_id($db)) does NOT return the id number just inserted.

This might seem like a dumb thing to do but I stumbled across this because the INSERT command was changed from not sending the record's ID number to setting it.  In either event (setting it or not setting it) - the id number should always be returned when the call is made to the insert_id() function after an INSERT command has been sent.


Test script:
---------------
MySQL:

Set up a simple table (called LOG) in a database with ID, Date, and Entry fields.

PHP:

Use a simple command to insert a record into the above table.

$table = mysqli( "HOST", "USER", "PASSWORD", "DATABASE" );
$sql = "insert into log (id, date, entry) values (1,NOW(),'Test')";
$table->query( $sql );
echo "ID = " . $table->insert_id . "\n";
exit;


Expected result:
----------------
ID = <Record ID>


Actual result:
--------------
ID = 0


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-27 21:09 UTC] markem at sim1 dot us
I forgot to put in that this is NOT the first record in the table.  It is record #60.  So you need to put in a few records to see that it still returns zero each time.
 [2015-05-27 21:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-05-27 21:18 UTC] requinix@php.net
You'll only get something if you trigger the generation of a new AUTO_INCREMENT value. Providing your own value, as you did with id=1, will not do that.
 [2015-05-29 01:15 UTC] markem at sim1 dot us
@requinix@php.net - Ah.  Got it.  May I suggest the record id be returned no matter what?  For consistency's sake.  Very confusing because there is no mention of this behavior in the documentation.

But - THANK YOU!  That resolved my problem perfectly.  I just keep the id now.  But I'd still like to put under suggestions that the id always be returned if possible.  No matter what the condition.  Of course - this would probably be more of a MySQL problem than PHP since it is probably MySQL that doesn't set the id number.  Hmmmmm....maybe I should pay them a visit.  :-)
 [2015-05-29 01:39 UTC] requinix@php.net
MySQL is the one making the decisions, actually, not PHP. We're just using their API.

https://dev.mysql.com/doc/refman/5.6/en/mysql-insert-id.html
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC