What are common pitfalls when using parametrized statements in PHP?

Common pitfalls when using parametrized statements in PHP include not properly binding parameters, not sanitizing input data, and not handling errors effectively. To avoid these issues, always bind parameters correctly using placeholders, sanitize user input to prevent SQL injection attacks, and handle exceptions or errors that may occur during the execution of the statement.

// Example of using parametrized statements in PHP with error handling

try {
    $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);
    $stmt->execute();
    
    // Fetch results
    while ($row = $stmt->fetch()) {
        // Process results
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}