How can error reporting be utilized to troubleshoot issues with database connections in PHP scripts?

When troubleshooting database connection issues in PHP scripts, error reporting can be utilized to provide more detailed information about the problem. By enabling error reporting, developers can see any errors or warnings that occur during the script execution, which can help pinpoint the root cause of the connection problem.

// Enable error reporting for database connection troubleshooting
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);
}