How can the syntax for INNER JOIN be properly implemented in a PHP script to avoid errors?

When implementing INNER JOIN in a PHP script, it is important to ensure that the SQL query syntax is correct to avoid errors. One common mistake is not specifying the table names properly in the JOIN clause. To avoid this, make sure to include the table names before the column names in the SELECT statement and properly define the relationship between the tables in the JOIN clause.

<?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 with INNER JOIN
$sql = "SELECT column1, column2
        FROM table1
        INNER JOIN table2 ON table1.id = table2.table1_id";

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