First of all, always have this line before mysqli connect in all your environments:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
After that all mysql errors will be transferred into PHP exceptions. Uncaught exception, in turn, makes a PHP fatal error. Thus, in case of mysql error, you'll get a conventional PHP error, that will instantly make you aware of the error cause. A stack trace will lead you to the exact spot where error occurred.
Note that you have to be able to see PHP errors in general. And here indeed goes the matter of different environments:
on a live site you have to peek into error logs, so, settings have to be
while on a local development server it's ok to make errors on screen:
And a little list of what you should not:
- use error suppression operator (
echo or any other function to print the error message on screen unconditionally. PHP can echo it all right already, no assistance required.
- test the query result manually (like
if($result)) it just makes no sense. Either your query failed and you will already get the error exception, or it was all right and there is nothing to test
- use try..catch for echoing error message. Again PHP can do it better, way better.