How can error reporting and display settings in PHP help in identifying and troubleshooting issues like database connection errors?
When encountering database connection errors in PHP, error reporting and display settings can help in identifying and troubleshooting the issue by providing detailed error messages. By setting the error reporting level to display all errors and warnings, developers can quickly pinpoint the source of the problem, such as incorrect credentials or server connectivity issues. Additionally, adjusting the display_errors setting to On ensures that error messages are shown directly on the webpage, aiding in real-time debugging.
// Set error reporting level to display all errors and warnings
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Adjust display_errors setting to show error messages on the webpage
ini_set('display_errors', 'On');
// Code for establishing a database connection
$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);
}