How can variables from multiple MySQL tables be output in PHP?

When needing to output variables from multiple MySQL tables in PHP, you can achieve this by performing a JOIN query in SQL to combine the relevant tables. This allows you to retrieve the necessary data in a single query and then output it using PHP.

<?php
// Connect to MySQL database
$connection = new mysqli("localhost", "username", "password", "database");

// Perform a JOIN query to retrieve data from multiple tables
$query = "SELECT table1.column1, table2.column2 FROM table1 JOIN table2 ON table1.id = table2.id";
$result = $connection->query($query);

// Output the retrieved data
while($row = $result->fetch_assoc()) {
    echo "Column 1: " . $row['column1'] . "<br>";
    echo "Column 2: " . $row['column2'] . "<br>";
}

// Close the database connection
$connection->close();
?>