How can PHP developers efficiently retrieve data from multiple tables in a database to display relevant information?

When retrieving data from multiple tables in a database, PHP developers can efficiently do so by using SQL JOIN queries to combine related data from different tables into a single result set. By specifying the columns to select and the tables to join on, developers can retrieve all the relevant information needed for display.

<?php
// Establish a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// SQL query to retrieve data from multiple tables
$sql = "SELECT users.username, orders.order_id, orders.total_amount
        FROM users
        INNER JOIN orders ON users.user_id = orders.user_id";

$result = $conn->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";
}

$conn->close();
?>