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 ..
3
  • Use @ and errors will not show up but you should prevent errors from happening Commented Jul 11, 2018 at 18:54
  • 7
    That will only display if you echo mysqli_connect_error($link);, so don't do that. Commented Jul 11, 2018 at 18:54
  • Possible duplicate of php hide ALL errors Commented Jul 11, 2018 at 19:06

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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.
Yes, i found that I'm printing the error in try catch with getMessage() method. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.