使用MYSQLI_REPORT_STRICT如何使mysqli抛出异常? [英] How to make mysqli throw exceptions using MYSQLI_REPORT_STRICT?
问题描述
在mysqli中有一个名为 mysqli_report()
的函数,看起来像PDO的 setAttribute()
方法与其 ERRMODE _ *
常量。手册说:
There is a function in mysqli, called mysqli_report()
, which looks like a counterpart for PDO's setAttribute()
method with its ERRMODE_*
constants. The manual says:
MYSQLI_REPORT_STRICT
抛出mysqli_sql_exception错误而不是警告
所以,考虑到 PDO :: ERRMODE_EXCEPTION
,我试过这个代码
So, having PDO::ERRMODE_EXCEPTION
in mind, i tried this code
mysqli_report(MYSQLI_REPORT_STRICT);
$mysqli->query("foo");
但是,令我失望的是,它根本不产生任何例外或警告。
but, to my disappointment, it produced no exception nor warning at all.
所以,这里有一个问题:有没有办法告诉mysqli抛出异常而不使用 MYSQLI_REPORT_ALL
?
So, here goes the question: is there a way to tell mysqli to throw exceptions without using MYSQLI_REPORT_ALL
?
推荐答案
经过一些研究,我终于知道函数的参数是一个位掩码,一个必须组合几个值来获得所需的结果。最终组合不是过于逻辑的,但它的工作原理,在忽略警告时抛出查询错误的异常。
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);
将产生所需的结果:
致命错误:未捕获的异常'mysqli_sql_exception'与消息'您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行'foo'附近使用正确的语法
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'
这篇关于使用MYSQLI_REPORT_STRICT如何使mysqli抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!