What is the purpose of mysql_insert_id in PHP and how is it used in database operations?
The purpose of mysql_insert_id in PHP is to retrieve the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. This function returns the ID of the last inserted record in the database. It is commonly used in database operations to retrieve the primary key value of the most recently inserted record.
// Connect to database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Insert data into table
mysqli_query($connection, "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')");
// Get the ID of the last inserted record
$inserted_id = mysqli_insert_id($connection);
// Use the ID for further operations
echo "The ID of the last inserted record is: " . $inserted_id;