What are the potential pitfalls of using LEFT JOIN in PHP for querying data from multiple tables?

Using LEFT JOIN in PHP for querying data from multiple tables can lead to potential pitfalls such as returning duplicate rows if there are multiple matches in the joined tables. To solve this issue, you can use the DISTINCT keyword in your SQL query to eliminate duplicate rows.

<?php
// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');

// Query with LEFT JOIN and DISTINCT to avoid duplicate rows
$query = "SELECT DISTINCT column1, column2 FROM table1 LEFT JOIN table2 ON table1.id = table2.table1_id";
$stmt = $pdo->query($query);

// Fetch and display results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}
?>