What are the potential pitfalls of mixing mysqli and PDO in PHP when handling database operations for user authentication?

Mixing mysqli and PDO in PHP for database operations can lead to confusion, inconsistency, and potential security vulnerabilities. It is recommended to choose one database extension (either mysqli or PDO) and stick with it throughout the project to maintain code readability and consistency. To solve this issue, it is best to refactor the code to use either mysqli or PDO exclusively for all database interactions.

// Using PDO for database operations
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error connecting to database: " . $e->getMessage());
}

// Example of user authentication using PDO
$username = $_POST['username'];
$password = $_POST['password'];

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

$user = $stmt->fetch();

if ($user) {
    echo "User authenticated successfully";
} else {
    echo "Invalid username or password";
}