How can error reporting in PHP help troubleshoot database connection issues?

When troubleshooting database connection issues in PHP, error reporting can help identify the specific problem that is causing the connection to fail. By enabling error reporting, you can see detailed error messages that can guide you in fixing the issue, such as incorrect credentials, server connectivity problems, or database configuration errors.

// Enable error reporting to display detailed error messages
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Your database connection code here
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}