What are the advantages of using PDO or mysqli over mysql_ functions in PHP?

Using PDO or mysqli over mysql_ functions in PHP offers several advantages, including improved security through prepared statements to prevent SQL injection attacks, support for multiple database systems, and object-oriented approach for better code organization and readability.

// Using PDO to connect to a MySQL database
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error connecting to database: " . $e->getMessage());
}