How can the error "Call to undefined function mysql_connect()" be resolved when encountered in PHP scripts?

The error "Call to undefined function mysql_connect()" occurs when trying to use the deprecated MySQL extension in PHP. This extension has been removed in newer PHP versions. To resolve this error, you should switch to either the MySQLi or PDO extension for database connections.

// Connect to MySQL using MySQLi extension
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";