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

One common pitfall when working with MySQL queries in PHP code is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to securely interact with the database. Additionally, make sure to handle errors effectively to troubleshoot any issues that may arise. Example PHP code snippet using prepared statements to prevent SQL injection:

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

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

// Bind the parameter to the query
$stmt->bindParam(':username', $_POST['username']);

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

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

// Handle any errors
if($stmt->errorCode() !== '00000') {
    $errorInfo = $stmt->errorInfo();
    echo "Error: " . $errorInfo[2];
}