How important is it to establish a database connection in PHP when working with MySQL?

Establishing a database connection in PHP when working with MySQL is crucial as it allows your PHP scripts to interact with the MySQL database. Without a proper database connection, you won't be able to perform any database operations such as querying, inserting, updating, or deleting data.

<?php
$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";
?>