What are the advantages of using PDO or MySQLi over the deprecated MySQL functions in PHP?

The deprecated MySQL functions in PHP are no longer recommended for use due to security vulnerabilities and lack of support. Instead, developers should use either PDO (PHP Data Objects) or MySQLi (MySQL Improved) for database operations in PHP. These newer extensions offer better security features, support for prepared statements, and are more versatile in terms of database support.

// 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();
}