What is the recommended approach for querying multiple tables in PHP, using JOIN or multi_query?

When querying multiple tables in PHP, it is recommended to use JOIN statements in your SQL queries rather than making multiple separate queries. JOINs allow you to combine data from multiple tables based on a related column, resulting in a more efficient and streamlined query. This approach reduces the number of queries sent to the database, improving performance and reducing the complexity of your code.

<?php
// Establish a connection to the database
$connection = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Query using JOIN to fetch data from multiple tables
$sql = "SELECT users.username, orders.order_id, orders.total_amount
        FROM users
        JOIN orders ON users.user_id = orders.user_id";

$result = $connection->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Username: " . $row["username"]. " - Order ID: " . $row["order_id"]. " - Total Amount: " . $row["total_amount"]. "<br>";
    }
} else {
    echo "0 results";
}

$connection->close();
?>