What are the benefits of using WHERE id IN(...) instead of individual DELETE queries in a loop for deleting multiple entries in a database using PHP?

When deleting multiple entries in a database using PHP, using a single query with WHERE id IN(...) is more efficient than running individual DELETE queries in a loop. This is because it reduces the number of database interactions, leading to better performance and faster execution.

// Assuming $ids is an array of IDs to be deleted
$ids = [1, 2, 3, 4];

// Construct the SQL query using WHERE id IN(...)
$sql = "DELETE FROM table_name WHERE id IN (" . implode(",", $ids) . ")";

// Execute the query
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "Entries deleted successfully.";
} else {
    echo "Error deleting entries: " . mysqli_error($conn);
}