What is the correct way to establish a MySQL connection in PHP?

To establish a MySQL connection in PHP, you need to use the mysqli_connect() function. This function takes four parameters: the hostname (usually "localhost"), the username, the password, and the database name. Once the connection is established, you can perform various database operations using this connection.

// Establishing a MySQL connection
$hostname = "localhost";
$username = "root";
$password = "";
$database = "my_database";

$conn = mysqli_connect($hostname, $username, $password, $database);

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