What are the advantages of using PDO over mysql_* functions for database connections in PHP?

Using PDO over mysql_* functions in PHP offers several advantages such as improved security through the use of prepared statements to prevent SQL injection attacks, support for multiple database vendors, and better error handling capabilities.

// Using PDO for database connection
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}