What are common syntax problems when using PHP for SQL queries?

One common syntax problem when using PHP for SQL queries is not properly escaping variables in the query string, which can lead to SQL injection vulnerabilities. To solve this issue, you should use prepared statements with parameterized queries to safely pass variables to the database.

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

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

// Bind the variable to the placeholder
$stmt->bindParam(':username', $username, PDO::PARAM_STR);

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

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