What are the advantages of using PDO or MySQLi over the deprecated mysql_* functions in PHP for database operations?

The mysql_* functions in PHP are deprecated and no longer recommended for use due to security vulnerabilities and lack of support for modern features. It is recommended to use PDO (PHP Data Objects) or MySQLi (MySQL Improved) for database operations as they provide more secure and flexible ways to interact with databases.

// Using PDO for database operations
$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);
    
    // Perform database operations using PDO
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

// Using MySQLi for database operations
$mysqli = new mysqli('localhost', 'username', 'password', 'mydatabase');

if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

// Perform database operations using MySQLi