Why is it recommended to avoid using mysql_* functions in PHP and what are some alternatives like mysqli or PDO?

It is recommended to avoid using mysql_* functions in PHP because they are deprecated and no longer maintained. Instead, it is recommended to use mysqli or PDO for database operations. These alternatives provide better security features, support for prepared statements, and are more compatible with modern PHP versions.

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

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

// Using PDO to connect to a MySQL database
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");

if (!$pdo) {
    die("Connection failed: " . $pdo->errorInfo());
}