PHP: Return ID of record just inserted?

C++, C#, Java, PHP, ect...
Post Reply
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

PHP: Return ID of record just inserted?

Post by OldRod »

I have a PHP script that inserts a record into a MySQL table. The table has an auto-increment ID field. Is it possible to capture the ID of the record that just got inserted and then return that back to the calling script?
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: PHP: Return ID of record just inserted?

Post by Xaos »

You could add time-stamps and look for the most recent one.
User avatar
Sharlenwar
Posts: 523
Joined: Mon May 28, 2012 7:14 pm

Re: PHP: Return ID of record just inserted?

Post by Sharlenwar »

You could use:

Code: Select all

echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.php

Not sure if that will help.
Deep within the Void of Quasion, a creation.

**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: PHP: Return ID of record just inserted?

Post by OldRod »

Sharlenwar wrote:You could use:

Code: Select all

echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.php

Not sure if that will help.
That looks promising. I will try that, thanks :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP: Return ID of record just inserted?

Post by Jackolantern »

OldRod wrote:
Sharlenwar wrote:You could use:

Code: Select all

echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.php

Not sure if that will help.
That looks promising. I will try that, thanks :)
Yeah, definitely try that. Sadly, this very common need is not fulfilled by the SQL standard, and so every vendor implements their own way to get it.
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP: Return ID of record just inserted?

Post by Callan S. »

Making a comment to put this on my comment record, for latter potential use. I've run into this, where I tried to give it a value then looked it up immediately after - fairly clunky method to deal with it.
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: PHP: Return ID of record just inserted?

Post by MikuzA »

My having that function, one thought crosses my mind.. I remember talking this with a buddy of mine and testing it or something..
But, does the function retrieve 'the last id of the inserted row in db' or 'the last id of the inserted row of the current connection/session in db'.

I remember doing a massive loop, opening it separate computers/browsers just to try breaking this to confirm which one it is..
Like inserting simulationiously huge amount of data and printing out the last id everytime..
Then just comparing the results towards the database..

What my results was that it was actually always 'that' sessions last id. Not sure thought if my test was sufficient enough.. and the answer to this might be found on the notes of this function..

Just adding it here for a precaution and discussion for further information! :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Sharlenwar
Posts: 523
Joined: Mon May 28, 2012 7:14 pm

Re: PHP: Return ID of record just inserted?

Post by Sharlenwar »

I used this to see how many items were insertted into my map database. I have a grid of 9x9 tiles and so there are 81 tiles. I used this to give me that number to confirm that it loaded in right.
Deep within the Void of Quasion, a creation.

**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
Post Reply

Return to “Coding”