捕捉Mysqli错误 [英] Catching Mysqli Errors

查看:89
本文介绍了捕捉Mysqli错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的错误处理程序定义,它捕获所有异常并将它们保存到一个日志中。现在,如果我在mysqli查询中有语法错误,比如打字错误,那么该页面就完全停止加载。抛出异常,因此错误处理程序不被触发,没有任何记录。



这是否正常?有没有PHP设置,我应该检查来修复这个,以便任何mysqli查询错误引发异常?



(如果查询返回0,我不希望抛出异常结果 - 只有由于查询结构中的打字错误或其他错误而导致错误)



示例查询:



< $($ r = $ result-> fetch_assoc()){$ data =$ ] = $ R;}} $ result->免费();

如果我尝试在PHPMyAdmin中执行此查询,它会告诉我列bad_field_reference不存在。如果我尝试执行它作为我的PHP脚本的一部分,整个页面停止加载在那一点。



澄清我只是在做通过查看页面源码进行一些测试。显然,页面的其余部分加载 - 但是,当启用java时,某些项目被隐藏,那么我使用jquery以动画方式重新显示一些内容。这些jquery脚本没有运行,所以该页面似乎已经停止加载。



所以 - 现在有2个问题 - 如何让PHP抓住错误,如何让jquery仍然运行它的脚本?

解决方案


这是正常吗?


否。


有没有PHP设置我应该检查以修复这个,以便任何mysqli查询错误抛出异常?


是。

$ b $ ($ MYSQLI_REPORT_STRICT)

但是,并不是所有的mysqli_ *函数都会引发DB错误,其中一些引发了常规的PHP错误,像 free()这样做,这部分让我想起更多:我也很好奇,为什么只处理例外,但是没有处理PHP错误。不要告诉我你有error_reporting(0); - 你吗?如果是这样 - 我没有话。



最后,我怀疑你有原始的mysqli-> query()分散在整个代码中。这意味着




  • 您没有占位符来构建查询 - 使SQL注入不可避免。

  • 你的代码blo肿和不可读



你必须使用一个抽象库将所有重复的代码转换为类方法。使用 safeMysql 查看运行的查询:

  $ data = $ db-> getAll('SELECT bad_field_reference FROM table'); 

它不需要水平滚动来阅读它,它是安全,简洁,防错误和支持占位符!


I have a custom error handler defined, which catches all exceptions and saves them to a log. Right now, if I have a syntax error in a mysqli query, such as a typo, the page completely stops loading at that point. No exception is thrown, so therefore the error handler isn't triggered and nothing is logged.

Is this normal? Is there a PHP setting that I should check to fix this so that any mysqli query errors throw exceptions?

(I don't want an exception thrown if a query returns 0 results - only if it errors out due to a typo or other error in the query structure)

Example query:

if($result=$db->query('SELECT bad_field_reference FROM table')){while($r=$result->fetch_assoc()){$data[]=$r;}}$result->free();

If I try to execute this query in PHPMyAdmin it will tell me that column bad_field_reference does not exist. If I try to execute it as part of my PHP script, the entire page stops loading at that point.

To clarify I was just doing some testing by looking at the page source. Apparently the rest of the page does load - however, when java is enabled certain items are hidden, then I use jquery to redisplay some content in an animated fashion. Those jquery scripts are not running, so the page appears to have stopped loading.

So - 2 questions now - how do I get PHP to 'catch' the error, and how do I get jquery to still run it's scripts?

解决方案

Is this normal?

No.

Is there a PHP setting that I should check to fix this so that any mysqli query errors throw exceptions?

Yes.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

However, not all the mysqli_* functions would raise a DB error, some of them raise regular PHP errors, like free() does, and this part makes me wonder even more: I am also extremely curious, why do you handle exceptions only but leave PHP errors unhandled. Don't tell me you have error_reporting(0); - do you? If so - I've no words.

Finally, I have suspicion that you have raw mysqli->query() scattered all over the code. Which means

  • you are using no placeholders to build your queries - making SQL injection inevitable.
  • your code is bloated and unreadable

You have to use an abstraction library that will put all the repeated code into class methods. See your query run with safeMysql:

$data = $db->getAll('SELECT bad_field_reference FROM table');

it require no horizontal scrolling to read it, it is safe, concise, error-proof and supports placeholders!

这篇关于捕捉Mysqli错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆