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

Using PDO over mysql_* functions in PHP is advantageous because PDO provides a more secure and flexible way to interact with databases. PDO supports multiple database drivers, which allows for easier database migration. Additionally, PDO uses prepared statements, which help prevent SQL injection attacks.

// 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);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}