Skip to content
PHP Logo

What is the purpose of the function mysql_insert_id() in PHP and how can it affect query results?

The purpose of the mysql_insert_id() function in PHP is to retrieve the ID generated by a query (usually an INSERT statement) on a table with an AUTO_INCREMENT column. This function can be used to get the ID of the last inserted record, which can be useful for further operations or for displaying to the user.

php
// Assuming a successful INSERT query has been executed
$last_insert_id = mysql_insert_id();
echo "The ID of the last inserted record is: " . $last_insert_id;