What could be the potential reasons for a MySQL database connection failure in PHP?

A potential reason for a MySQL database connection failure in PHP could be incorrect database credentials, server connection issues, or the MySQL server being down. To solve this issue, double-check the database credentials, ensure the MySQL server is running, and verify that the server allows remote connections.

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