What are the key differences in syntax and functionality between PDO and mysqli in PHP when it comes to handling database connections and queries?

When it comes to handling database connections and queries in PHP, PDO and mysqli are two commonly used extensions. PDO is more versatile as it supports multiple database types, while mysqli is specifically designed for MySQL databases. PDO uses a consistent object-oriented approach, while mysqli uses a procedural style.

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