What is the function in PHP to retrieve the last inserted ID in a database?
To retrieve the last inserted ID in a database using PHP, you can use the `mysqli_insert_id()` function. This function returns the ID generated by a query on a table with a column having the `AUTO_INCREMENT` attribute. It is commonly used after an `INSERT` query to get the ID of the last inserted record.
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Perform an INSERT query
mysqli_query($conn, "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')");
// Get the last inserted ID
$last_id = mysqli_insert_id($conn);
// Output the last inserted ID
echo "Last Inserted ID: " . $last_id;