What are the advantages of using PDO over mysqli in PHP for more efficient and readable code when working with databases?

When working with databases in PHP, using PDO over mysqli can lead to more efficient and readable code. PDO provides a more consistent interface for accessing different types of databases, making it easier to switch between database systems. Additionally, PDO supports prepared statements, which help prevent SQL injection attacks and improve performance.

// Using PDO to connect to a MySQL database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}