What are common syntax errors to avoid when writing PHP code for SQL queries?

One common syntax error to avoid when writing PHP code for SQL queries is not properly escaping variables to prevent SQL injection attacks. To solve this issue, you should use prepared statements with parameterized queries to safely pass variables to the database.

// Avoid SQL injection by using prepared statements
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();