What are the recommended alternatives to using the deprecated MySQL extension in PHP for database operations?

The recommended alternatives to using the deprecated MySQL extension in PHP for database operations are to use either MySQLi (MySQL Improved) or PDO (PHP Data Objects) extensions. Both MySQLi and PDO provide more secure and feature-rich ways to interact with databases compared to the deprecated MySQL extension.

// Using MySQLi extension
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');

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

// Perform database operations using MySQLi

$mysqli->close();
```

```php
// Using PDO extension
$dsn = 'mysql:host=localhost;dbname=database_name';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die('Connection failed: ' . $e->getMessage());
}

// Perform database operations using PDO

$pdo = null;