What are common errors encountered when using PHP for database queries, and how can they be resolved?

One common error when using PHP for database queries is not properly sanitizing user input, which can lead to SQL injection attacks. To resolve this, always use prepared statements with parameterized queries to prevent malicious input from affecting your database.

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