What are the benefits of using PDO or MySQLi as replacements for the deprecated MySQL extension in PHP?

The issue with using the deprecated MySQL extension in PHP is that it is no longer supported and poses security risks. To solve this problem, developers should switch to either PDO (PHP Data Objects) or MySQLi (MySQL Improved) extensions, which provide more secure and flexible ways to interact with databases.

// 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) {
    die("Connection failed: " . $e->getMessage());
}