How to make mysqli throw exceptions using MYSQLI_REPORT_STRICT? [duplicate]

After some research I've finally learned that the function's parameter is a bitmask, and one have to combine several values to get the desired result. The final combination is not overly logical, but it works as intended, throwing an exception on a query error while ignoring warnings.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

will produce the desired result:

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'foo' at line 1'


That's not a bug, that's a feature. ;)

PHP does not report mysqli or PDO errors by default because that information is highly sensitive, displaying it to a user is a great way to learn how to inject malicious data.

MYSQLI_REPORT_ERROR tells it to turn on the errors and MYSQLI_REPORT_STRICT tells it to convert those errors into Exceptions. This will give you a full report of the error message, so if you do this in production make sure that you do not display it to the end user.

Using the Pipe symbol | allows you to set multiple constants in most of PHPs methods and functions. PDO, mysqli, filter_var, etc. all use the pipe to set multiple optional arguments of the same type, or a "bitwise disjunction of flags" to use the fancy term for it. The lazy person's array argument.