Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Using MySQL with PHP Wish List | Report Bug | About Me ]

9.4.8     Reading auto-incrementing values: mysql_insert_id()

This is NOT the latest copy of this book; click here for the latest version.

int mysql_insert_id ( [resource link_identifier])

In the table specification for dogbreeds, the ID field was an INT AUTO_INCREMENT PRIMARY KEY. This means that MySQL will automatically assign increasingly higher integers to the ID field for us as INSERT queries are sent - but how can we tell what numbers it is using?

There are two ways to read the last-used auto-increment value: using a query, or calling a function.

The query option relies on the special MAX() function of MySQL. As MySQL will assign increasingly higher numbers to the ID field, the way to find the most recently assigned number is to run code like this:

mysql_query("SELECT ID AS MAX(ID) FROM dogbreeds;");

The alternative is to use the function mysql_insert_id(), which will return the last ID auto-inserted by the current connection. There is a subtle difference there, and one that makes it important enough for you to learn both methods of retrieving auto-incrementing values.

The difference lies in the fact that mysql_insert_id() returns the last ID number that MySQL issued for this connection, regardless of what other connections are doing. Furthermore, mysql_insert_id() only stores one value - the last ID number that MySQL issued for this connection on any table. On the other hand, using the SQL query allows you to check the very latest ID that has been inserted, even if you have not run any queries or if it is been 20 minutes since your last query. Furthermore, you can use the query on any table you like, which makes it even more useful.

As both do roughly the same thing, you have your choice. It is faster, and quite frankly easier to use mysql_insert_id() if you just want to know the ID number that MySQL used for your last INSERT operation. Generally speaking you will find mysql_insert_id() is what you will use the most, but do try to keep the other method at least somewhere in the back of your head, because it is much more flexible than using mysql_insert_id().





<< 9.4.7 Advanced formatting   9.4.9 Unbuffered queries for large data sets: mysql_unbuffered_query() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
ravi - 06 Sep 2008

Further on this, if you want to retrieve the most recently inserted row (as I did), you can do it with the help of a subquery:

mysql_query("select * from dogbreeds where id=(SELECT MAX(ID) FROM dogbreeds);");

ravi - 06 Sep 2008

Further on this, if you want to retrieve the most recently inserted row (as I did), you can do it with the help of a subquery:

mysql_query("select * from dogbreeds where id=(SELECT MAX(ID) FROM dogbreeds);");

Romain Gaucher - 06 Sep 2008

Actually, this is good if you don't have any gap in your ID: I mean unique ID removed.
If you have some, you have to read the next id of your table.

The code should be like this:

Perform the query,
SHOW TABLE STATUS LIKE 'YoutTable'

then, get the 'Auto_increment' value of the array

Cheers,
Romain

GogotheGreat, Kingstown, Serbia - 06 Sep 2008

January 25, 2006

In the given example for mysql_query() approach, the query itself is misspelled.

It should read like this:

mysql_query("SELECT MAX(ID) as ID FROM dogbreeds;");


Thanks, Paul for THE BEST online PHP manual ever written!!! I'm becoming the fucking expert for PHP, MySQL, Apache... u name it - thanks to You! ;)

kavitenyo@gmail.com - 06 Sep 2008

more on the special MAX() function of MySQL.

just put the name of the auto-incremented field inside the (), this tip are for newbies like me. enjoy coding!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow