What are common syntax errors in PHP code that can lead to SQL syntax errors?
Common syntax errors in PHP code that can lead to SQL syntax errors include not properly escaping special characters in SQL queries and not concatenating variables correctly within the query string. To avoid SQL syntax errors, always use prepared statements with parameterized queries to prevent SQL injection attacks and ensure proper escaping of user input. Example PHP code snippet implementing the fix:
```php
// Correct way to use prepared statements with parameterized queries
$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();
```
In this example, the use of prepared statements with parameterized queries ensures that user input is properly escaped and prevents SQL syntax errors.