What are some best practices for connecting to a database in PHP, considering the deprecated mysql functions?

The best practice for connecting to a database in PHP, considering the deprecated mysql functions, is to use the MySQLi or PDO extension. These extensions provide a more secure and flexible way to interact with databases in PHP. By using prepared statements and parameterized queries, you can prevent SQL injection attacks and ensure the safety of your database interactions.

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

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

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