How can PHP handle multiple IDs in a single query and differentiate between them for output?
When handling multiple IDs in a single query, you can use the IN clause in SQL to specify multiple IDs. In PHP, you can dynamically generate the list of IDs and pass it to the query. To differentiate between the IDs in the output, you can use a loop to iterate over the results and process each ID individually.
<?php
// Assume $ids is an array of IDs
$ids = [1, 2, 3, 4];
// Generate a comma-separated list of IDs
$idList = implode(',', $ids);
// Construct the SQL query with the IN clause
$query = "SELECT * FROM table WHERE id IN ($idList)";
// Execute the query and process the results
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
// Differentiate between IDs and output the results
echo "ID: " . $row['id'] . " - Data: " . $row['data'] . "<br>";
}
?>