How can PHP beginners effectively query data from multiple tables in a MySQL database and retrieve specific information based on a common attribute?

To query data from multiple tables in a MySQL database and retrieve specific information based on a common attribute, beginners can use SQL JOIN statements to combine data from different tables. By specifying the common attribute in the JOIN condition, the query can fetch related data from multiple tables in a single query. This allows for more efficient data retrieval and reduces the need for multiple separate queries.

<?php

// Establish a connection to the MySQL 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);
}

// Query to retrieve specific information from multiple tables based on a common attribute
$sql = "SELECT table1.column1, table2.column2
        FROM table1
        JOIN table2 ON table1.common_attribute = table2.common_attribute
        WHERE table1.common_attribute = '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();

?>