Why is it recommended to avoid using the mysql_* extension in PHP for database operations and what are the alternative options available?

The mysql_* extension in PHP is deprecated and has been removed in newer versions of PHP due to security vulnerabilities and lack of support. It is recommended to use either MySQLi (MySQL Improved) or PDO (PHP Data Objects) extensions for database operations. Both MySQLi and PDO offer more secure and flexible ways to interact with databases.

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

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

// Using PDO extension
try {
    $pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}