1

Based on articles that I read I made few lines that I include everywhere right before mysqli_connect:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
if ($admin)
{
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
}
else
{
    ini_set('display_errors', 0);
    ini_set('display_startup_errors', 0);
    ini_set('log_errors', 1);
}

After some testing everything seems to work perfect - for admin it throws sweet, long error reports. However, for user it simply kills scripts - which is what I want, but I'd like to give some information to user, that something went wrong. I assume, that it will be an universal message, or can I customize it to tell user what went wrong? Even if not - I'd like to throw there at least some "whops" for user - but how to do that?

Zorann
  • 335
  • 2
  • 16
  • 1
    Most likely off topic: Handle `mysqli_report` with care.. Before PHP `5.2.15` mysqli_report is per process and not per request.. So if your webserver is configured to use one PHP process you can get wrong reports from other requests also the fix is to use `mysqli_report(MYSQLI_REPORT_OFF)` when you done with getting data from MySQL. – Raymond Nijland Feb 02 '19 at 14:23
  • `of ($admin) { ini_set('display_errors', 1); ini_set('display_startup_errors', 1); }` whats `of` there i assume this should be `if`? – Raymond Nijland Feb 02 '19 at 14:27
  • Yes, I'm before 5.2.15, yet as You can see - I'm just learning how to handle things at all, not to mention of how to do it with care... Could You suggest something? Articles that I'm reading are lacking of crucial informations, and here You suggest to turn reports off? – Zorann Feb 02 '19 at 14:27
  • Yes, it was just an typo – Zorann Feb 02 '19 at 14:28
  • i advice you to take a look into [PDO](https://stackoverflow.com/questions/8992795/set-pdo-to-throw-exceptions-by-default) which supports the same.. `PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION` those driver options can be set to throw exceptions – Raymond Nijland Feb 02 '19 at 14:34
  • I'm not familiar with mysqli but you should be able to trap both [legacy errors](http://php.net/manual/en/function.set-error-handler.php) and [exceptions](http://php.net/manual/en/function.set-exception-handler.php) in a custom application-wide error handler that takes care of aborting gracefully. – Álvaro González Feb 02 '19 at 14:52
  • will not work @ÁlvaroGonzález you need to fetch MySQLi error yourself with `mysqli_error()` and throw a exception on error.. Meaning ideally you could program your own class to handle the reporting.and make it working with a error/exception hander like you said. – Raymond Nijland Feb 02 '19 at 14:58
  • @RaymondNijland Then what's `MYSQLI_REPORT_STRICT` for? – Álvaro González Feb 02 '19 at 15:04
  • "Then what's MYSQLI_REPORT_STRICT for?" It does basically the same as PDO's `PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION` @ÁlvaroGonzález like i already comment but `mysqli_report()` can't be "trusted" before `PHP 5.2.15` which the topicstarter is using.. That why i adviced him into using PDO driver options instead off MySQLi.`mysqli_report()` – Raymond Nijland Feb 02 '19 at 15:07
  • 1
    @RaymondNijland Ah, got it now, thank you. – Álvaro González Feb 02 '19 at 15:09
  • Just to check - you say you are on pre - PHP 5.2.15 - which was stopped being supported in 2011. My advice would be to try and upgrade ASAP. – Nigel Ren Feb 02 '19 at 15:19
  • Yeah... currently it doesn't depend on me unfortunately. But I will if this will change ;) – Zorann Feb 02 '19 at 16:11

0 Answers0