How can SQL injection vulnerabilities be mitigated in PHP code, especially in scenarios involving user authentication?

SQL injection vulnerabilities can be mitigated in PHP code by using prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to prevent malicious SQL code from being injected into the query and executed.

```php
// Using prepared statements with parameterized queries
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(array('username' => $username, 'password' => $password));
```

By using prepared statements and parameterized queries, the user input is treated as data rather than SQL code, making it safer and protecting against SQL injection attacks.