Why is it recommended to switch from using the mysql extension to mysqli or PDO in PHP?

It is recommended to switch from using the mysql extension to mysqli or PDO in PHP because the mysql extension is deprecated and no longer maintained, making it vulnerable to security risks and compatibility issues with newer versions of PHP. Mysqli and PDO offer more features, better security, and improved performance for interacting with databases.

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";