What are common pitfalls when trying to link two tables from a shared database using HTML and PHP?

One common pitfall when trying to link two tables from a shared database using HTML and PHP is not properly establishing the database connection before querying the tables. To solve this issue, make sure to include the database connection code at the beginning of your PHP script.

<?php
// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query the tables and link them as needed
$sql = "SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.table1_id";
$result = $conn->query($sql);

// Display the linked data in HTML table
if ($result->num_rows > 0) {
    echo "<table><tr><th>Column1</th><th>Column2</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["column1"]."</td><td>".$row["column2"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

$conn->close();
?>