What are some potential pitfalls to be aware of when joining tables in PHP for displaying data?

One potential pitfall when joining tables in PHP for displaying data is the risk of SQL injection if user input is not properly sanitized. To prevent this, always use prepared statements when executing SQL queries that involve user input.

// Example of using prepared statements to join tables in PHP
$pdo = new PDO("mysql:host=localhost;dbname=myDB", $username, $password);

// Assume $userId is user input
$userId = $_GET['userId'];

// Prepare the SQL query with placeholders
$stmt = $pdo->prepare("SELECT * FROM users u JOIN orders o ON u.id = o.user_id WHERE u.id = :userId");

// Bind the parameter to the placeholder
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);

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

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

// Display the data
foreach ($results as $row) {
    echo $row['username'] . ' - ' . $row['order_details'] . '<br>';
}