How can PHP developers efficiently integrate multiple SQL queries into a single output format for improved user experience and readability?
To efficiently integrate multiple SQL queries into a single output format in PHP, developers can use loops to iterate through the results of each query and combine them into a single array or object. This approach helps improve user experience by presenting all relevant data in a unified format, making it easier to read and understand.
// Execute multiple SQL queries
$query1 = "SELECT * FROM table1";
$query2 = "SELECT * FROM table2";
$result1 = mysqli_query($conn, $query1);
$result2 = mysqli_query($conn, $query2);
// Combine results into a single output format
$output = array();
while ($row = mysqli_fetch_assoc($result1)) {
$output['table1'][] = $row;
}
while ($row = mysqli_fetch_assoc($result2)) {
$output['table2'][] = $row;
}
// Output the combined results
echo json_encode($output);