What are the common pitfalls to avoid when working with MySQL queries in PHP?

One common pitfall to avoid when working with MySQL queries in PHP is not properly sanitizing user input, which can leave your application vulnerable to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries to ensure that user input is properly escaped.

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