Home  >  Q&A  >  body text

How to disable PHP reporting SQL errors?

<p>I have my own PHP data class that logs MySQL errors to a txt file using <code>mysqli_error($this->conn)</code></p> <p>Everything was working fine until I set PHP error handling to E_ALL. Now PHP intercepts the error and it doesn't have a chance to be handled by my MySQL error handler. </p> <p>This wouldn't be a problem except that PHP didn't log the full SQL string of the error. It only logs the first 20 characters or so, followed by "...", which isn't very useful for debugging. </p> <p>So my question is: is it possible to tell PHP to ignore MySQL errors while still handling PHP errors? </p> <p>I've looked at various levels of error_reporting() but nothing seems to specifically leave MySQL errors. </p>
P粉550257856P粉550257856409 days ago602

reply all(1)I'll reply

  • P粉161939752

    P粉1619397522023-09-06 12:49:44

    You can do this by disabling mysqli error reporting, but this shouldn't be done for any regular project. In fact, it looks like you're going about this problem the wrong way! You shouldn't want to disable error reporting, you should fix errors!

    As you mentioned, if the error comes from an incorrectly constructed dynamic SQL statement, then your code should not contain any logic to handle this possible scenario because you don't want the error to be part of the design. Just like any other error, this is a bug in the PHP code. The fact that it manifests itself as a MySQL error is irrelevant. You must fix the code that builds the invalid SQL statement! Silencing errors does not help fix errors!

    Assuming that your PHP code builds SQL from the constant SQL part (if it uses variables, then there will be bigger problems in your code), then your task is to determine which code path is causing the SQL keyword to be incorrect combination. You can partially infer this from the SQL errors. The exception message will tell you what the syntax error is. If the MySQL error is not related to broken syntax, the process is still similar: find the cause of the error and fix it.

    Don't write special error handling logic around SQL code. It's confusing and absolutely unnecessary.

    reply
    0
  • Cancelreply