How to get the last created auto-increment ID from a MySQL table
If you’re like me then you’re developing like crazy. Once in a while you may need to retrieve the auto-increment ID value from the last created row you inserted into a particular mysql table. Yea… when you insert a new row into a mysql table there is no apparent way to just return the ID from the row’s auto-increment field. So to do this using PHP, simply use the
functin, which will return the last created ID as an interger.
The only catch is that you will want to run this function RIGHT after you do your insert, else some other process might create a new row after yours which would prevent you from obtaining the correct ID.
Anyways, short and sweet but saved a bunch of time on my end!