How can PHP developers prevent SQL injection attacks when handling passwords?

To prevent SQL injection attacks when handling passwords in PHP, developers should use prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to sanitize input data and prevent malicious SQL code from being executed.

// Using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();