How can error reporting be effectively utilized in PHP to troubleshoot connection issues?
When troubleshooting connection issues in PHP, error reporting can be effectively utilized by setting the error reporting level to display all errors, warnings, and notices. This can help identify the specific error causing the connection problem, such as incorrect credentials or server configuration issues. By enabling error reporting, developers can quickly pinpoint the issue and take appropriate steps to resolve it.
// Enable error reporting to display all errors, warnings, and notices
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Your PHP code for establishing a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}