PHP: Return ID of record just inserted?
PHP: Return ID of record just inserted?
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?
Re: PHP: Return ID of record just inserted?
You could add time-stamps and look for the most recent one.
- Sharlenwar
- Posts: 523
- Joined: Mon May 28, 2012 7:14 pm
Re: PHP: Return ID of record just inserted?
You could use:
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.php
Not sure if that will help.
Code: Select all
echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
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
**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG
Re: PHP: Return ID of record just inserted?
That looks promising. I will try that, thanksSharlenwar wrote:You could use:
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.phpCode: Select all
echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
Not sure if that will help.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: PHP: Return ID of record just inserted?
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.OldRod wrote:That looks promising. I will try that, thanksSharlenwar wrote:You could use:
Reference: http://ca3.php.net/manual/en/pdo.lastinsertid.phpCode: Select all
echo "Last Insert Id: " . $db->lastInsertId() . "<br>";
Not sure if that will help.
The indelible lord of tl;dr
Re: PHP: Return ID of record just inserted?
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.
Re: PHP: Return ID of record just inserted?
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!
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
Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
- Sharlenwar
- Posts: 523
- Joined: Mon May 28, 2012 7:14 pm
Re: PHP: Return ID of record just inserted?
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
**My Corner of the Web**
***NEW***GrindFest - My own PHP/MySQL game!
Sharlenwar's Adventures
Torn-City - Massively multiplayer online text based RPG