How can the mysql_insert_id() function be used to return the last inserted ID in the provided PHP code?
To return the last inserted ID in PHP, you can use the mysql_insert_id() function which retrieves the ID generated for an AUTO_INCREMENT column by the previous query. After executing an INSERT query, you can call mysql_insert_id() to get the ID of the last inserted row in the database.
// Execute your INSERT query here
$inserted_id = mysql_insert_id($connection); // $connection is your database connection variable
echo "Last inserted ID: " . $inserted_id;
Related Questions
- What are the potential pitfalls of not using cookies in PHP session management and relying solely on database-based session tracking?
- What are common challenges faced when generating a CSV file from a database in PHP?
- How can PHP developers ensure that the processing of service start and stop requests does not impact the user experience on the website?