What are common pitfalls when using PDO prepare statements for database queries in PHP?

One common pitfall when using PDO prepare statements is not properly binding parameters, which can lead to SQL injection vulnerabilities. To avoid this, always use placeholders in the query and bind parameters using the bindParam or bindValue methods.

// Example of using PDO prepare statement with parameter binding
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$username = "john_doe";
$stmt->execute();

// Fetch results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);