What steps should be taken to properly configure a SQL database connection in PHP scripts to prevent connection failures?

To properly configure a SQL database connection in PHP scripts to prevent connection failures, you should ensure that the database credentials are correct, handle potential errors gracefully, and use secure methods to connect to the database.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_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";
?>