What are some potential pitfalls of using multiple MySQL tables in a PHP script without proper table linking?

Using multiple MySQL tables in a PHP script without proper table linking can lead to data inconsistencies and errors in querying data. To solve this issue, it is important to establish relationships between the tables using foreign keys and JOIN queries to ensure that data is accurately retrieved and updated across different tables.

// Establish a connection to the MySQL database
$conn = new mysqli($servername, $username, $password, $dbname);

// Query to select data from multiple tables using JOIN
$sql = "SELECT table1.column1, table2.column2 FROM table1 JOIN table2 ON table1.id = table2.table1_id";

// Execute the query and fetch the results
$result = $conn->query($sql);

// Loop through the results and display the data
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}

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