Home > Backend Development > PHP Tutorial > Why Don't MySQLi Queries Throw Exceptions Even with `mysqli_report(MYSQLI_REPORT_STRICT)`?

Why Don't MySQLi Queries Throw Exceptions Even with `mysqli_report(MYSQLI_REPORT_STRICT)`?

Patricia Arquette
Release: 2024-12-31 07:24:11
Original
265 people have browsed it

Why Don't MySQLi Queries Throw Exceptions Even with `mysqli_report(MYSQLI_REPORT_STRICT)`?

Exceptions for MySQLi Query Errors

Question

Despite setting mysqli_report(MYSQLI_REPORT_STRICT), query errors in MySQLi don't throw exceptions. mysqli_sql_exception is only thrown for connection errors. Is it normal to manually check for mysqli_query()'s return value to detect query failures?

Answer

Yes, manual checking is often necessary because:

  1. mysqli_report(): Calling mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); does enable exception throwing.
  2. Try-catch: Contrary to popular belief, exceptions should not be used excessively. Most errors should be handled centrally by a site-wide error handler.

Former code:

$result = mysqli_query($DBlink, $SQL);
if($result === false) {
    throw new MySQLiQueryException($SQL, mysqli_error($DBlink), mysqli_errno($DBlink));
}
Copy after login

Instead, only use try-catch sparingly for errors that require immediate attention.

The above is the detailed content of Why Don't MySQLi Queries Throw Exceptions Even with `mysqli_report(MYSQLI_REPORT_STRICT)`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template