What potential pitfalls should be avoided when writing SQL queries in PHP?

One potential pitfall to avoid when writing SQL queries in PHP is SQL injection attacks. To prevent this, you should always use prepared statements with bound parameters instead of directly inserting user input into your queries.

// Example of using prepared statements to avoid SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();