What are common pitfalls when creating SQL queries in PHP, as seen in the provided code snippet?

Common pitfalls when creating SQL queries in PHP include not properly sanitizing user input, leaving the code vulnerable to SQL injection attacks. To solve this issue, you should always use prepared statements with parameterized queries to prevent SQL injection.

// Using prepared statements to prevent SQL injection
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

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

// Bind parameters
$stmt->bindParam(':username', $username);

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

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