MySQL Get Last ID

MySQL Get Last ID


Get ID of The Last Inserted Record

In table an unique ID for the AUTO_INCREMENT column each time you insert a new record or row into the table. However, there are certain situations when you need that automatically generated ID to insert it into a second table. In these situations you can use the PHP mysqli_insert_id() function to retrieve the most recently generated ID,as shown in the upcoming example.

The following examples are equal to the examples from the previous page (MySQL Insert Data ), except that we have added one single line of code to retrieve the ID of the last inserted record. We also echo the last.

Example (MySQLi Object-oriented)

Output :

Example (MySQLi Procedural):

Output :

Example (PDO)

Output :