What are the advantages of transitioning from the old MySQL extension to PDO or mysqli for database interactions in PHP scripts?

Transitioning from the old MySQL extension to PDO or mysqli in PHP scripts offers several advantages, including improved security through the use of prepared statements to prevent SQL injection attacks, support for multiple database types, and better error handling capabilities. Additionally, PDO and mysqli are more actively maintained and supported by the PHP community compared to the deprecated MySQL extension.

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