What is the significance of properly establishing a database connection in PHP scripts?

Properly establishing a database connection in PHP scripts is crucial for ensuring that the script can interact with the database to retrieve or store data. Without a valid connection, the script will not be able to execute queries or perform any database operations. To establish a connection, you need to provide the correct credentials, such as the database host, username, password, and database name.

// Database credentials
$host = "localhost";
$username = "root";
$password = "";
$database = "my_database";

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

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