Why is it recommended to use PDO or mysqli instead of the deprecated mysql extension in PHP?

The mysql extension in PHP is deprecated and no longer supported, making it vulnerable to security risks and lacking in modern features. It is recommended to use PDO or mysqli extensions, which provide more secure and efficient ways to interact with databases.

// 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);
    echo "Connected to database successfully";
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}