<bdo id='RRKbx'></bdo><ul id='RRKbx'></ul>

  • <tfoot id='RRKbx'></tfoot>
    1. <small id='RRKbx'></small><noframes id='RRKbx'>

    2. <i id='RRKbx'><tr id='RRKbx'><dt id='RRKbx'><q id='RRKbx'><span id='RRKbx'><b id='RRKbx'><form id='RRKbx'><ins id='RRKbx'></ins><ul id='RRKbx'></ul><sub id='RRKbx'></sub></form><legend id='RRKbx'></legend><bdo id='RRKbx'><pre id='RRKbx'><center id='RRKbx'></center></pre></bdo></b><th id='RRKbx'></th></span></q></dt></tr></i><div id='RRKbx'><tfoot id='RRKbx'></tfoot><dl id='RRKbx'><fieldset id='RRKbx'></fieldset></dl></div>

      1. <legend id='RRKbx'><style id='RRKbx'><dir id='RRKbx'><q id='RRKbx'></q></dir></style></legend>
      2. 我可以用 PHP 检测和处理 MySQL 警告吗?

        时间:2023-09-21

        <small id='Mt9rn'></small><noframes id='Mt9rn'>

        <tfoot id='Mt9rn'></tfoot>
        <i id='Mt9rn'><tr id='Mt9rn'><dt id='Mt9rn'><q id='Mt9rn'><span id='Mt9rn'><b id='Mt9rn'><form id='Mt9rn'><ins id='Mt9rn'></ins><ul id='Mt9rn'></ul><sub id='Mt9rn'></sub></form><legend id='Mt9rn'></legend><bdo id='Mt9rn'><pre id='Mt9rn'><center id='Mt9rn'></center></pre></bdo></b><th id='Mt9rn'></th></span></q></dt></tr></i><div id='Mt9rn'><tfoot id='Mt9rn'></tfoot><dl id='Mt9rn'><fieldset id='Mt9rn'></fieldset></dl></div>
          <bdo id='Mt9rn'></bdo><ul id='Mt9rn'></ul>
              <tbody id='Mt9rn'></tbody>

                  <legend id='Mt9rn'><style id='Mt9rn'><dir id='Mt9rn'><q id='Mt9rn'></q></dir></style></legend>

                  本文介绍了我可以用 PHP 检测和处理 MySQL 警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在处理一个 MySQL 表,该表将 JobName 列定义为 UNIQUE.如果有人尝试使用数据库中已有的 JobName 将新 Job 保存到数据库中,MySQL 会抛出警告.

                  I'm dealing with a MySQL table that defines the JobName column as UNIQUE. If somebody tries to save a new Job to the database using a JobName that is already in the database, MySQL throws a warning.

                  我希望能够在我的 PHP 脚本中检测到这个警告,就像一个错误一样,并进行适当的处​​理.理想情况下,我想知道 MySQL 抛出了什么样的警告,以便我可以分支代码来处理它.

                  I would like to be able to detect this warning, just like an error, in my PHP script and deal with it appropriately. Ideally I would like to know what kind of warning MySQL has thrown so that I can branch the code to handle it.

                  这可能吗?如果没有,是因为MySQL没有这个能力,PHP没有这个能力,还是两者都有?

                  Is this possible? If not, is it because MySQL doesn't have this ability, PHP doesn't have this ability, or both?

                  推荐答案

                  对于本地标记"到 PHP 的警告需要更改 mysql/mysqli 驱动程序,这显然超出了本问题的范围.相反,您将不得不基本上检查您对数据库所做的每个查询是否有警告:

                  For warnings to be "flagged" to PHP natively would require changes to the mysql/mysqli driver, which is obviously beyond the scope of this question. Instead you're going to have to basically check every query you make on the database for warnings:

                  $warningCountResult = mysql_query("SELECT @@warning_count");
                  if ($warningCountResult) {
                      $warningCount = mysql_fetch_row($warningCountResult );
                      if ($warningCount[0] > 0) {
                          //Have warnings
                          $warningDetailResult = mysql_query("SHOW WARNINGS");
                          if ($warningDetailResult ) {
                              while ($warning = mysql_fetch_assoc($warningDetailResult) {
                                  //Process it
                              }
                          }
                      }//Else no warnings
                  }
                  

                  显然,使用整体应用会非常昂贵,因此您可能需要仔细考虑警告可能出现的时间和方式(这可能会导致您进行重构以消除它们).

                  Obviously this is going to be hideously expensive to apply en-mass, so you might need to carefully think about when and how warnings may arise (which may lead you to refactor to eliminate them).

                  作为参考,MySQL 显示警告

                  当然,您可以省去对 SELECT @@warning_count 的初始查询,这样可以为您每次执行节省一个查询,但我为了迂腐的完整性而包含它.

                  Of course, you could dispense with the initial query for the SELECT @@warning_count, which would save you a query per execution, but I included it for pedantic completeness.

                  这篇关于我可以用 PHP 检测和处理 MySQL 警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:set_error_handler() 对致命错误不起作用 下一篇:如何在 Laravel 中手动返回或抛出验证错误/异常

                  相关文章

                  最新文章

                    <bdo id='76hNY'></bdo><ul id='76hNY'></ul>

                  1. <i id='76hNY'><tr id='76hNY'><dt id='76hNY'><q id='76hNY'><span id='76hNY'><b id='76hNY'><form id='76hNY'><ins id='76hNY'></ins><ul id='76hNY'></ul><sub id='76hNY'></sub></form><legend id='76hNY'></legend><bdo id='76hNY'><pre id='76hNY'><center id='76hNY'></center></pre></bdo></b><th id='76hNY'></th></span></q></dt></tr></i><div id='76hNY'><tfoot id='76hNY'></tfoot><dl id='76hNY'><fieldset id='76hNY'></fieldset></dl></div>
                    <legend id='76hNY'><style id='76hNY'><dir id='76hNY'><q id='76hNY'></q></dir></style></legend>

                    <small id='76hNY'></small><noframes id='76hNY'>

                    1. <tfoot id='76hNY'></tfoot>