0

Is there any way to prevent MySql errors (if happened) from appearing to the client, and just log them somewhere ?

Note 1: Am not using any frameworks, just native PHP with PDO extinsion and MySql.

Note 2: This code will stop PHP errors, not MySql.

ini_set('display_errors', 0);
ini_set("display_startup_errors", 0);
error_reporting(0);

Example:

disabling errors like

SQLSTATE[HY000] [1045] Access denied for user ..
shamaseen
  • 2,193
  • 2
  • 21
  • 35

1 Answers1

0

For non-PHP errors, the application (programmer) is responsible for obtaining and displaying/logging the error. So for your specific example, don't display it, log it:

//instead of displaying
if (!$link) {
    die(mysqli_connect_error());
}

//log it
if (!$link) {
    error_log(mysqli_connect_error());
}

You could also us trigger_error that would respect your error reporting settings:

if (!$link) {
    trigger_error(mysqli_connect_error(), E_USER_ERROR);
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • So in the case of PDO there is "PDO::ATTR_ERRMODE", but i notice that give it PDO::ERRMODE_SILENT value still show sql errors when happens .. is there a way to stop it ? – shamaseen Jul 11 '18 at 19:19
  • 1
    `PDO::ERRMODE_SILENT` is default and does not display, you would need to set `PDO::ERRMODE_WARNING` or `PDO::ERRMODE_EXCEPTION` to have it display something. Somwhere the code is either displaying the error or telling PHP to generate one. – AbraCadaver Jul 11 '18 at 19:23
  • Yes, i found that I'm printing the error in try catch with getMessage() method. Thank you ! – shamaseen Jul 11 '18 at 19:40