Why is it recommended to avoid using the mysql extension in PHP?

The mysql extension in PHP is deprecated and has been removed as of PHP 7.0. It is recommended to use the mysqli or PDO extensions instead, as they offer more features, better security, and support for prepared statements which help prevent SQL injection attacks.

// Using mysqli extension to connect to a MySQL database
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');

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

// Perform database operations using $mysqli object