How can SQL injection vulnerabilities be prevented when passing user input as parameters in PHP scripts?

SQL injection vulnerabilities can be prevented by using prepared statements and parameterized queries in PHP scripts. This helps to separate SQL code from user input, preventing malicious SQL queries from being executed.

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

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

// Bind the user input to the placeholders
$stmt->bindParam(':username', $_POST['username']);

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

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