How can beginners avoid using deprecated functions like mysql_connect in PHP?

Deprecated functions like mysql_connect in PHP should be avoided by using newer and more secure alternatives like MySQLi or PDO. Beginners can update their code to use these modern functions to ensure compatibility with the latest PHP versions and to improve security.

// Using MySQLi to connect to a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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