What are common syntax errors to watch out for when writing SQL queries in PHP?

Common syntax errors to watch out for when writing SQL queries in PHP include missing quotation marks around strings, forgetting to escape special characters, and incorrect table or column names. To avoid these errors, always double-check your query syntax and use prepared statements to prevent SQL injection attacks. Example PHP code snippet using prepared statements:

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

// Prepare a SQL query using placeholders
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");

// Bind parameter values to placeholders
$stmt->bindParam(':username', $username);

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

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

// Loop through results
foreach ($results as $row) {
    echo $row['username'] . "<br>";
}