What are the key differences between mysqli and PDO in PHP, and how do they impact code functionality?

The key differences between mysqli and PDO in PHP lie in their flexibility and portability. While mysqli is specific to MySQL databases, PDO can work with multiple database systems. This impacts code functionality as PDO allows for easier migration between different database platforms without needing to rewrite queries, making it a more versatile choice.

// 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);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}