What are the potential pitfalls of using UNION, UNION ALL, and LEFT JOIN for database searches in PHP?

One potential pitfall of using UNION, UNION ALL, and LEFT JOIN for database searches in PHP is the risk of SQL injection attacks if user input is not properly sanitized. To prevent this, always use prepared statements with parameterized queries to securely handle user input.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();