What best practices should be followed when establishing a connection to a MySQL database in PHP?

When establishing a connection to a MySQL database in PHP, it is important to follow best practices to ensure security and efficiency. This includes using parameterized queries to prevent SQL injection attacks, using a dedicated user with limited privileges, and properly sanitizing user input.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_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";
?>