What are common errors that can occur when using PHP to query a database?

One common error when querying a database with PHP is not properly escaping user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to sanitize input.

// Example of using prepared statements to query a database safely
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();