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

    1. <legend id='wiGeH'><style id='wiGeH'><dir id='wiGeH'><q id='wiGeH'></q></dir></style></legend>

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

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

        检查 PDO Fetch select 语句何时返回 null

        时间:2023-10-05

        1. <legend id='xbusb'><style id='xbusb'><dir id='xbusb'><q id='xbusb'></q></dir></style></legend>
          <tfoot id='xbusb'></tfoot>

            • <bdo id='xbusb'></bdo><ul id='xbusb'></ul>

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

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

                    <tbody id='xbusb'></tbody>
                  本文介绍了检查 PDO Fetch select 语句何时返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下代码:

                    $check = $dbh->prepare("SELECT * FROM BetaTesterList WHERE EMAIL = ?");
                                  $check->execute(array($email));
                                  $res = $check->fetchAll();
                  
                                  if (!($res['EMAIL'])){
                                          $stmt = $dbh->prepare("INSERT INTO BetaTesterList(EMAIL) VALUES (?)");
                                          $stmt->execute(array($email));
                                  } else {
                                          $return['message'] = 'exists';
                                  }
                  

                  然而,尽管该记录已存在于数据库中,但这仍会插入该值.我如何防止这种情况?

                  However this still inserts the value although the record already exists in the DB. How do I prevent this?

                  推荐答案

                  这里有几件事...

                  1. PDOStatement::fetchAll() 返回一个数组数组.要检查记录,请尝试

                  1. PDOStatement::fetchAll() returns an array of arrays. To check for a record, try

                  if (count($res) == 0) {
                      // no records found
                  }
                  

                1. 开启 E_NOTICE 错误.您应该知道 $res['EMAIL'] 是一个未定义的索引.在脚本的顶部...

                2. Turn on E_NOTICE errors. You would have known that $res['EMAIL'] was an undefined index. At the top of your script...

                  ini_set('display_errors', 'On');
                  error_reporting(E_ALL);
                  

                3. 我建议为您的 EMAIL 列创建唯一约束.这样,您将无法插入重复的记录.如果尝试,PDO 将触发错误或抛出异常,具体取决于您如何配置 PDO::ATTR_ERRMODE 属性(请参阅 http://php.net/manual/en/pdo.setattribute.php)

                4. I'd recommend creating a unique constraint on your EMAIL column. That way, you would not be able to insert a duplicate record. If one was attempted, PDO would trigger an error or throw an exception, depending on how you configure the PDO::ATTR_ERRMODE attribute (see http://php.net/manual/en/pdo.setattribute.php)

                  如果您不想这样做,请考虑改用此查询...

                  If you're not inclined to do so, consider using this query instead...

                  $check = $dbh->prepare("SELECT COUNT(1) FROM BetaTesterList WHERE EMAIL = ?");
                  $check->execute(array($email));
                  $count = $check->fetchColumn();
                  
                  if ($count == 0) {
                      // no records found
                  } else {
                      // record exists
                  }
                  

                5. 这篇关于检查 PDO Fetch select 语句何时返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 PHP 对象设置为全局? 下一篇:了解 PDO 准备好的语句和绑定参数

                  相关文章

                  最新文章

                6. <small id='UZyR4'></small><noframes id='UZyR4'>

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

                  <tfoot id='UZyR4'></tfoot>

                  1. <legend id='UZyR4'><style id='UZyR4'><dir id='UZyR4'><q id='UZyR4'></q></dir></style></legend>
                    • <bdo id='UZyR4'></bdo><ul id='UZyR4'></ul>