How important is it to properly set up and configure a database connection in PHP scripts?

Properly setting up and configuring a database connection in PHP scripts is crucial for ensuring secure and efficient data retrieval and manipulation. This involves providing the correct database credentials, handling errors gracefully, and using prepared statements to prevent SQL injection attacks.

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