How can PHP be used to efficiently handle and process data from multiple tables in a database?

To efficiently handle and process data from multiple tables in a database using PHP, you can use SQL JOIN queries to retrieve data from multiple tables based on a common key. This allows you to fetch related data from different tables in a single query, reducing the number of database calls and improving performance.

<?php
// Connect to the database
$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 using JOIN
$sql = "SELECT orders.order_id, customers.customer_name, products.product_name
        FROM orders
        JOIN customers ON orders.customer_id = customers.customer_id
        JOIN products ON orders.product_id = products.product_id";

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

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Order ID: " . $row["order_id"]. " - Customer Name: " . $row["customer_name"]. " - Product Name: " . $row["product_name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>