What are the advantages of using PDO over mysql/mysqli in PHP?

When working with databases in PHP, it is recommended to use PDO (PHP Data Objects) over the traditional mysql/mysqli extensions. PDO offers a more secure and flexible way to interact with databases, supports multiple database types, and provides a consistent interface for database operations.

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