What potential pitfalls can arise when passing parameters with single quotes in PHP PDO for SQL queries?

Passing parameters with single quotes in PHP PDO for SQL queries can lead to SQL injection vulnerabilities if not properly handled. To avoid this issue, it's recommended to use prepared statements and bind the parameters securely. This way, the values will be automatically escaped by PDO, preventing any malicious SQL injection attacks.

// Example of using prepared statements with PDO to pass parameters securely

// Database connection
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

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

// Bind the parameter securely
$username = "john'; DROP TABLE users;";
$stmt->bindParam(':username', $username, PDO::PARAM_STR);

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

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

// Loop through the results
foreach($results as $row) {
    echo $row['username'] . "<br>";
}