What are some common pitfalls when using PDO in PHP for database queries?

One common pitfall when using PDO in PHP for database queries is not properly handling errors. It's important to check for errors after executing queries and handle them gracefully to prevent unexpected behavior. Another pitfall is not using prepared statements, leaving your code vulnerable to SQL injection attacks. Always use prepared statements to securely pass user input to your database queries.

// Example of handling errors in PDO
try {
    // Execute query
    $stmt = $pdo->query("SELECT * FROM users");
} catch (PDOException $e) {
    // Handle error
    echo "Error: " . $e->getMessage();
}

// Example of using prepared statements in PDO
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();