What are some common errors that can occur when executing SQL queries in PHP, and how can they be resolved?

One common error when executing SQL queries in PHP is not properly escaping input data, which can lead to SQL injection attacks. To resolve this, you should always use prepared statements with parameterized queries to prevent malicious input from affecting your database.

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

// Prepare a SQL query with a placeholder for the input data
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind the input data to the placeholder
$stmt->bindParam(':username', $username);

// Execute the query
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll();