What are the common errors that can occur when establishing a connection to a MySQL database using mysqli_connect in PHP?

Common errors that can occur when establishing a connection to a MySQL database using mysqli_connect in PHP include incorrect server hostname, username, password, or database name, as well as issues with the MySQL server itself. To solve this, double-check the connection parameters and ensure they are correct. Additionally, make sure the MySQL server is running and accessible.

$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";

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

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