What are the advantages of using prepared statements in PHP for login authentication compared to direct SQL queries?

Using prepared statements in PHP for login authentication is more secure compared to direct SQL queries because it helps prevent SQL injection attacks. Prepared statements separate SQL logic from user input, making it harder for attackers to manipulate the query. Additionally, prepared statements can improve performance as they allow the database to optimize query execution.

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

if($stmt->rowCount() > 0) {
    // Login successful
} else {
    // Login failed
}