What are the advantages of using PDO or mysqli_ with prepared statements in PHP?

Using PDO or mysqli_ with prepared statements in PHP helps prevent SQL injection attacks by separating the SQL query from the user input. Prepared statements also improve performance as the query can be prepared once and executed multiple times with different parameters. Additionally, prepared statements make the code more readable and maintainable by clearly separating the query from the data.

// Using PDO with prepared statements
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();