What are the benefits of using PDO or MySQLi over the deprecated MySQL extension in PHP?

The deprecated MySQL extension in PHP has been removed in PHP 7.0 and is no longer supported. To continue working with databases in PHP, it is recommended to use either PDO (PHP Data Objects) or MySQLi (MySQL Improved) extensions. Both PDO and MySQLi offer more features, better security, and support for prepared statements, making them more secure and efficient options for interacting with databases in PHP.

// Using PDO to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}