How can the code provided be improved to prevent SQL injection vulnerabilities?

To prevent SQL injection vulnerabilities, you should use prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This approach separates SQL code from user input, preventing malicious input from altering the query's structure.

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

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

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

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

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