How can PHP developers protect their code from SQL injection vulnerabilities when querying user login information?

To protect PHP code from SQL injection vulnerabilities when querying user login information, developers should use prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to prevent malicious SQL injection attacks by automatically escaping special characters in user input.

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

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

// Bind parameters to the prepared statement
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);

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

// Fetch the result
$user = $stmt->fetch();