How can SQL injection vulnerabilities be mitigated when executing SQL queries in PHP?

SQL injection vulnerabilities can be mitigated by using prepared statements with bound parameters in PHP. This technique ensures that user input is properly escaped and prevents malicious SQL queries from being executed.

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

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

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

// Execute the prepared statement
$stmt->execute();

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