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

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

      1. <legend id='j8OtE'><style id='j8OtE'><dir id='j8OtE'><q id='j8OtE'></q></dir></style></legend>
        • <bdo id='j8OtE'></bdo><ul id='j8OtE'></ul>
      2. 带有 foreach 和 fetch 的 PHP PDO

        时间:2023-09-23

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

                • <small id='D8Kdj'></small><noframes id='D8Kdj'>

                  本文介绍了带有 foreach 和 fetch 的 PHP PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  以下代码:

                  <?php
                  try {
                      $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
                      echo "Connection is successful!<br/>";
                      $sql = "SELECT * FROM users";
                      $users = $dbh->query($sql);
                      foreach ($users as $row) {
                          print $row["name"] . "-" . $row["sex"] ."<br/>";
                      }
                      foreach ($users as $row) {
                          print $row["name"] . "-" . $row["sex"] ."<br/>";
                      }
                      $dbh = null;
                  }
                  catch (PDOexception $e) {
                      echo "Error is: " . $e-> etmessage();
                  }
                  

                  输出:

                  Connection is successful!
                  
                  person A-male
                  person B-female
                  

                  运行foreach"两次不是我的目的,我只是好奇为什么两个foreach"语句只输出一次结果?

                  Running "foreach" twice is not my purpose, I'm just curious why TWO "foreach" statements only output the result once?

                  以下是类似的情况:

                  <?php
                  try {
                      $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
                      echo "Connection is successful!<br/>";
                      $sql = "SELECT * FROM users";
                      $users = $dbh->query($sql);
                      foreach ($users as $row) {
                          print $row["name"] . "-" . $row["sex"] ."<br/>";
                      }
                      echo "<br/>";
                      $result = $users->fetch(PDO::FETCH_ASSOC);
                      foreach($result as $key => $value) {
                          echo $key . "-" . $value . "<br/>";
                      }
                      $dbh = null;
                  }
                  catch (PDOexception $e) {
                      echo "Error is: " . $e-> etmessage();
                  }
                  

                  输出:

                  Connection is successful!
                  
                  person A-male
                  person B-female
                  
                  SCREAM: Error suppression ignored for
                  Warning: Invalid argument supplied for foreach()
                  

                  但是当我从上面的代码中删除第一个foreach"时,输出会变得正常:

                  But when I delete the first "foreach" from the above codes, the output will become normal:

                  <?php
                  try {
                      $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
                      echo "Connection is successful!<br/>";
                      $sql = "SELECT * FROM users";
                      $users = $dbh->query($sql);
                  
                      echo "<br/>";
                      $result = $users->fetch(PDO::FETCH_ASSOC);
                      foreach($result as $key => $value) {
                          echo $key . "-" . $value . "<br/>";
                      }
                      $dbh = null;
                  }
                  catch (PDOexception $e) {
                      echo "Error is: " . $e-> etmessage();
                  }
                  

                  输出:

                  Connection is successful!
                  
                  user_id-0000000001
                  name-person A
                  sex-male
                  

                  为什么会发生这种情况?

                  Why does this happen?

                  推荐答案

                  A PDOStatement(您在 $users 中有)是一个前向光标.这意味着,一旦使用(第一次 foreach 迭代),它就不会倒回到结果集的开头.

                  A PDOStatement (which you have in $users) is a forward-cursor. That means, once consumed (the first foreach iteration), it won't rewind to the beginning of the resultset.

                  可以在foreach之后关闭游标,再次执行语句:

                  You can close the cursor after the foreach and execute the statement again:

                  $users       = $dbh->query($sql);
                  foreach ($users as $row) {
                      print $row["name"] . " - " . $row["sex"] . "<br/>";
                  }
                  
                  $users->execute();
                  
                  foreach ($users as $row) {
                      print $row["name"] . " - " . $row["sex"] . "<br/>";
                  }
                  

                  或者你可以使用定制的 CachingIterator 和完整缓存来缓存:

                  Or you could cache using tailored CachingIterator with a fullcache:

                  $users       = $dbh->query($sql);
                  
                  $usersCached = new CachedPDOStatement($users);
                  
                  foreach ($usersCached as $row) {
                      print $row["name"] . " - " . $row["sex"] . "<br/>";
                  }
                  foreach ($usersCached as $row) {
                      print $row["name"] . " - " . $row["sex"] . "<br/>";
                  }
                  

                  您找到CachedPDOStatement 类作为要点.缓存迭代器可能比将结果集存储到数组中更合理,因为它仍然提供它所包装的 PDOStatement 对象的所有属性和方法.

                  You find the CachedPDOStatement class as a gist. The caching iterator is probably more sane than storing the result set into an array because it still offers all properties and methods of the PDOStatement object it has wrapped.

                  这篇关于带有 foreach 和 fetch 的 PHP PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP:限制 foreach() 语句? 下一篇:$k => 是什么意思?$v in foreach($ex as $k=>$v) 是什

                  相关文章

                  最新文章

                • <tfoot id='58lPI'></tfoot>

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

                    <small id='58lPI'></small><noframes id='58lPI'>

                    • <bdo id='58lPI'></bdo><ul id='58lPI'></ul>