What advantages does PDO offer over mysqli when it comes to database connectivity in PHP?

PDO offers several advantages over mysqli when it comes to database connectivity in PHP. One major advantage is that PDO supports multiple database systems, allowing you to switch between different databases without changing your code. PDO also provides a more secure way to interact with the database by using prepared statements, which helps prevent SQL injection attacks. Additionally, PDO has a more consistent and object-oriented interface compared to mysqli.

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