How can PHP be used to efficiently retrieve data from multiple tables based on a common identifier?

When retrieving data from multiple tables based on a common identifier in PHP, you can use SQL JOIN queries to efficiently fetch the related data in a single query. By using JOINs, you can combine data from multiple tables based on a shared column or key. This approach reduces the number of queries needed to retrieve the desired data and improves performance.

<?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);
}

// Query to retrieve data from multiple tables based on a common identifier
$sql = "SELECT table1.column1, table2.column2
        FROM table1
        JOIN table2 ON table1.common_id = table2.common_id
        WHERE table1.common_id = 'value'";

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

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>