What are common syntax errors to watch out for in MySQL queries when using PHP?

One common syntax error to watch out for in MySQL queries when using PHP is forgetting to properly escape variables in the query string. This can lead to SQL injection attacks and syntax errors. To solve this issue, always use prepared statements with placeholders and bind the variables to prevent SQL injection.

// Example of using prepared statements to prevent SQL injection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll();