Log database errors instead of showing them.

display_errors is off unless DISPLAY_ERRORS is set in .env.
This commit is contained in:
tim
2026-09-25 21:57:11 +02:00
parent bcba2e1ca3
commit f97bdecbc1
9 changed files with 40 additions and 16 deletions
+19 -2
View File
@@ -13,8 +13,9 @@ function open_db($state) {
mysqli_report(MYSQLI_REPORT_OFF);
$db = new mysqli($hostname, $view_user, $view_pass, $db_name);
if ($db->connect_error) {
die ("Unable to connect to database");
die ('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error);
$detail = 'connect error (' . $db->connect_errno . ') ' . $db->connect_error;
$db = NULL;
db_fail($detail);
}
return TRUE;
} else
@@ -29,4 +30,20 @@ function open_db($state) {
return FALSE;
}
}
/*
* Stop the page after a database error. The details, with the file and
* line of the failed call, go to the PHP error log; the visitor only
* sees a short apology.
*/
function db_fail($detail = NULL) {
global $db;
if ($detail === NULL)
$detail = $db ? $db->error : 'no database connection';
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
error_log('xxx: database error at ' . basename($caller['file']) . ':' . $caller['line'] . ': ' . $detail);
if (!headers_sent())
header('HTTP/1.1 500 Internal Server Error');
die('<p>Sorry, something went wrong. Please try again later.</p>');
}
?>