• <bdo id='KoSZL'></bdo><ul id='KoSZL'></ul>
  • <legend id='KoSZL'><style id='KoSZL'><dir id='KoSZL'><q id='KoSZL'></q></dir></style></legend>

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

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

        尝试访问 bool 类型值的数组偏移量

        时间:2023-09-19

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

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

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

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

                    <tbody id='B4JtN'></tbody>
                  本文介绍了尝试访问 bool 类型值的数组偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  $query = $pdo -> prepare("SELECT * FROM Users WHERE Username =:Username");
                  $query->bindParam(':Username', $name);
                  $query->execute();
                  
                  $nameRes = $query->fetch(PDO::FETCH_ASSOC);
                  if ($nameRes['Username']==$_POST['username']) {
                      die ("Username is already in use!");
                  }   
                  
                  $query = $pdo -> prepare("SELECT * FROM Users WHERE Email =:Email");
                  $query->bindParam(':Email', $email);
                  $query ->execute();
                  $emailRes = $query->fetch(PDO::FETCH_ASSOC);
                  
                  if ($emailRes['Email']==$_POST['email']) {
                      die ("Email is already in use!");
                  }
                  

                  我在我的应用程序的注册页面上有这个代码,当用户名可以免费使用但电子邮件不是,反之亦然我得到这个

                  I have this code on the registration page of my app and when Username is free to use but email is not and vice versa I get this

                  注意:尝试访问 bool 类型值的数组偏移量

                  Notice: Trying to access array offset on value of type bool

                  好的,结果返回false,但在这种情况下该怎么办?注意:这是在 php v7.4 上,同样的事情在 v7.3 上工作

                  Ok the result is returning false but what to do in this situation? Note: This is on php v7.4 this same thing was working on v7.3

                  推荐答案

                  您收到此错误可能是因为在数据库中找不到符合您条件的记录.

                  解决此错误的最简单方法是先检查数据库是否返回任何内容.

                  The easiest way to solve this error is to check if the database returned anything first.

                  $emailRes = $query->fetch(PDO::FETCH_ASSOC);
                  // VVV - Here I am checking if there was anything returned and then I check the condition
                  if($emailRes && $emailRes['Email']==$_POST['email']) {
                      // ...
                  }
                  

                  如果您不在乎数据库是否返回任何内容,那么您可以简单地提供一个默认值.例如:

                  If you don't care whether the database returned anything, then you can simply provide a default value. For example:

                  $emailRes = $query->fetch(PDO::FETCH_ASSOC);
                  $email = $emailRes['Email'] ?? ''; // default: empty string
                  

                  使用 PDO 检查 DB 中是否存在的正确方法是:

                  The correct way to check for existance in DB using PDO is:

                  $query = $pdo->prepare("SELECT COUNT(*) FROM Users WHERE Username =:Username");
                  $query->execute([':Username' => $name]);
                  if ($query->fetchColumn()) {
                      throw new Exception("Username is already in use!");
                  }
                  
                  $query = $pdo->prepare("SELECT COUNT(*) FROM Users WHERE Email =:Email");
                  $query->execute([':Email' => $email]);
                  if ($query->fetchColumn()) {
                      throw new Exception("Email is already in use!");
                  }
                  

                  我没有在 PHP 中获取行并再次进行比较,而是从数据库中获取匹配行的计数,并将该计数用作 if 语句中的布尔值.fetchColumn() 将从第一行获取单列,如果我使用 COUNT(*) 我知道总会有一行.

                  Instead of fetching the row and doing the comparison again in PHP I am fetching a count of matching rows from the database and I use that count as a boolean in the if statement. fetchColumn() will fetch a single column from the first row and if I use COUNT(*) I know there will always be one row.

                  您也可以在一个查询中完成:

                  You can also do it in one query:

                  $query = $pdo->prepare("SELECT COUNT(*) FROM Users WHERE Username =:Username OR  Email =:Email");
                  $query->execute([':Username' => $name, ':Email' => $email]);
                  if ($query->fetchColumn()) {
                      throw new Exception("Username or email is already in use!");
                  }
                  

                  这篇关于尝试访问 bool 类型值的数组偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 PDO 准备好的语句中检索(或模拟)完整查询 下一篇:从 mysql_connect() 获取 PHP PDO 连接?

                  相关文章

                  最新文章

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

                • <tfoot id='aH0Z5'></tfoot>

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

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

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