What are the potential syntax errors that can occur when passing SQL queries from PHP to a database?

One potential syntax error that can occur when passing SQL queries from PHP to a database is not properly escaping special characters in the query string. This can lead to SQL injection attacks or errors in the query execution. To solve this issue, you should use prepared statements with parameterized queries to securely pass data to the database.

// Example of using prepared statements to prevent SQL injection

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

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

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

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

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