What are some common pitfalls when working with MySQL variables in PHP?

One common pitfall when working with MySQL variables in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries when interacting with the database. Another pitfall is not checking for errors when executing queries, which can result in unexpected behavior or data loss. Always check for errors after executing queries to ensure the operation was successful.

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

```php
// Example of checking for errors after executing a query
$stmt = $pdo->query("SELECT * FROM users");
if($stmt === false){
    die("Error executing query");
}