What are some potential pitfalls to avoid when using PHP to display linked data from multiple database tables?

One potential pitfall to avoid when displaying linked data from multiple database tables in PHP is not properly sanitizing user input, which can leave the application vulnerable to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries when interacting with the database.

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare a SQL statement using a parameterized query
$stmt = $pdo->prepare("SELECT * FROM table1 JOIN table2 ON table1.id = table2.table1_id WHERE table1.id = :id");

// Bind the parameter value
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Execute the statement
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Display the linked data
foreach ($results as $row) {
    echo $row['column1'] . ' ' . $row['column2'] . '<br>';
}