P粉4209586922023-08-25 10:43:00
No.
You should be able to follow your requirements and instruct the mysqli driver to throw an exception on SQL errors, but you will need to enable MYSQLI_REPORT_ERROR
if it is not already enabled....
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)
mysqli_query()
Should now throw an exception on error. You don't need to check the return value on failure (the exception will be thrown anyway).
public function mysqlQuery($SQL) { try { $this->Result = mysqli_query($this->DBlink, $SQL); } catch (mysqli_sql_exception $e) { throw new MySQLiQueryException($SQL, $e->getMessage(), $e->getCode()); } return $this->Result; }
(Note: I changed $this->SQL
to $SQL
in the rethrown exception.)
P粉5494120382023-08-25 00:06:47
Some time ago I successfully solved this problem. As pointed out in other answers,
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
is the correct way to tell mysqli to throw an exception.
Just make sure not to wrap try-catch in every query. This is a very common misconception, once you start using exceptions you should start throwing try and catch everywhere. In contrast, try-catch should be used with caution. Although 99% of errors should not be handled on site but by a site-wide error handler. You can learn more about this topic from my PHP Error Reporting article.