Why is it recommended to avoid using the MySQL extension in PHP and switch to mysqli or PDO?

Using the MySQL extension in PHP is not recommended because it is deprecated and no longer maintained, making it vulnerable to security risks. It is advised to switch to either the mysqli extension or PDO (PHP Data Objects) for improved security, performance, and support for newer MySQL features.

// Using mysqli extension
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Using PDO
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);