How can you retrieve the ID of the entry created in a MySQL database using PHP?
When inserting data into a MySQL database using PHP, you can retrieve the ID of the entry created by using the mysqli_insert_id() function. This function returns the auto-generated ID used in the last query. After inserting the data into the database, you can call mysqli_insert_id() to get the ID of the newly created entry.
// Insert data into the database
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
$result = mysqli_query($connection, $query);
// Get the ID of the newly created entry
$new_id = mysqli_insert_id($connection);
echo "The ID of the new entry is: " . $new_id;
Related Questions
- What are some recommended resources or documentation for handling database queries in PHP?
- What are some recommended resources or tools for debugging PHP applications with numerous scripts?
- How can one troubleshoot and debug cURL POST requests in PHP to identify issues like server responses or request formatting?