What are the advantages and disadvantages of using PDO vs. MySQLi for database connections in PHP?

When choosing between PDO and MySQLi for database connections in PHP, PDO is often preferred for its flexibility and support for multiple database types, while MySQLi is specifically designed for MySQL databases and may offer slightly better performance. However, PDO requires more code to achieve the same functionality as MySQLi, which can be a disadvantage for simpler projects.

// Using PDO for database connection
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

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