MySQL DatabaseLesson 62Free preview
MySQL Get Last ID
This lesson is part of PHP · All levels
Summary
After you add a row, you often need to know the automatic id that you just created to use as a relation to another table. PHP provides the mysqli_insert_id() function that returns the last AUTO_INCREMENT value of the connection. This function is only accurate if it is called right after the successful INSERT command.
Key points
- Mysqli_insert_id($connection) returns the last ID of the INSERT result to that connection.
- The value comes from the AUTO_INCREMENT column, not the regular SELECT.
- The results are most accurate when called right after the INSERT is successful.
- The ID is useful as a foreign key on the associated table.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.