What is the recommended alternative to using the deprecated MySQL extension in PHP?

The recommended alternative to using the deprecated MySQL extension in PHP is to use either the MySQLi (MySQL Improved) extension or PDO (PHP Data Objects) extension. Both of these extensions provide more modern and secure ways to interact with MySQL databases in PHP.

// 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());
}