How can multiple tables be queried from a database in PHP?

When querying multiple tables from a database in PHP, you can use SQL JOIN statements to combine data from different tables based on a related column. By specifying the columns to select and the tables to join in the SQL query, you can retrieve data from multiple tables in a single query.

<?php
// Establish a connection 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 t1.column1, t2.column2
        FROM table1 t1
        JOIN table2 t2 ON t1.common_column = t2.common_column";

$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();
?>