How can SQL Injection vulnerabilities be mitigated when querying passwords from a database in PHP?

SQL Injection vulnerabilities can be mitigated by using prepared statements and parameterized queries when querying passwords from a database in PHP. This helps prevent malicious users from injecting SQL code into the query, which could potentially expose sensitive information such as passwords.

// Using prepared statements to query passwords from a database in PHP
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

$username = $_POST['username'];

$stmt = $pdo->prepare("SELECT password FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();

$result = $stmt->fetch();
if ($result) {
    $password = $result['password'];
    // Use the password retrieved from the database
} else {
    // Username not found
}