What are common pitfalls when using PHP for database queries, as seen in the forum thread?

Common pitfalls when using PHP for database queries include not sanitizing user input, not using prepared statements to prevent SQL injection attacks, and not properly handling errors. To solve these issues, always sanitize user input before using it in a query, use prepared statements with placeholders for dynamic data, and implement error handling to catch any potential issues.

// 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();