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

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

        <bdo id='wgagk'></bdo><ul id='wgagk'></ul>
      <tfoot id='wgagk'></tfoot>
    2. 如何在 PDO fetchAll 中正确使用 while 循环

      时间:2023-09-19

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

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

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

            <tbody id='bDRbh'></tbody>

              <i id='bDRbh'><tr id='bDRbh'><dt id='bDRbh'><q id='bDRbh'><span id='bDRbh'><b id='bDRbh'><form id='bDRbh'><ins id='bDRbh'></ins><ul id='bDRbh'></ul><sub id='bDRbh'></sub></form><legend id='bDRbh'></legend><bdo id='bDRbh'><pre id='bDRbh'><center id='bDRbh'></center></pre></bdo></b><th id='bDRbh'></th></span></q></dt></tr></i><div id='bDRbh'><tfoot id='bDRbh'></tfoot><dl id='bDRbh'><fieldset id='bDRbh'></fieldset></dl></div>
              • <bdo id='bDRbh'></bdo><ul id='bDRbh'></ul>
                本文介绍了如何在 PDO fetchAll 中正确使用 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                请放轻松,我刚开始学习 PDO 并且仍在寻找如何将我的 mysqli 转换为 PDO 的方法.

                please be easy on me, i just started learning PDO and still finding my way how to convert my mysqli to PDO.

                所以我有一个函数可以从我的数据库中获取内容

                so i have a function to get the contents from my database

                function getContent() {
                    $db = PDOconn();
                    $query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
                    $sql = $db->prepare($sql);
                    $row = $sql->fetchAll(PDO::FETCH_ASSOC);
                
                    return $row;
                }
                

                通常当我在mysqli中返回$row时,我会在while循环中定义fetch_assoc().

                normally when i return $row in mysqli, i would define fetch_assoc() in my while loop.

                while ($row = $result->fetch_assoc())  {
                    $id = $row['id'];
                    $content = $row['content'];
                }
                

                现在,因为 (PDO::FETCH_ASSOC) 已经在我的函数中声明了.

                Now, since (PDO::FETCH_ASSOC) is already declared in my function.

                我将如何正确创建我的 while 循环来打印 PDO 中的值?

                how would i properly create my while loop to print the values in PDO?

                更新代码

                我将在函数之外声明我的 while 循环.所以我需要一些东西从我的函数中返回,但我不知道那是什么..

                i will be declaring my while loop outside of the function. so i need something to return from my function but i dont know what that is..

                function getContent() {
                    $db = PDOconn();
                    $query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
                    $sql = $db->prepare($query);
                    $row = $sql->execute();
                
                    return $row;
                }
                

                这是我在函数外的while循环.

                this is my while loop outside the function.

                $sql = getContent();
                
                while ($row = $sql->fetchAll(PDO::FETCH_ASSOC))  {
                    $id = $row['id'];
                    $content = $row['content'];
                }
                

                推荐答案

                使用 fetchAll() 你不必使用 while根本.由于此函数返回一个数组,您必须改用foreach():

                With fetchAll() you don't have to use while at all. As this function returns an array, you have to use foreach() instead:

                function getContent() {
                    $db = PDOconn();
                    $query = "SELECT * FROM posts ORDER BY id DESC LIMIT 0,3";
                    $sql = $db->prepare($query);
                    $sql->execute();
                    return $sql->fetchAll();
                }
                
                $data = getContent();
                foreach($data as $row) {
                    $id = $row['id'];
                    $content = $row['content'];
                }
                

                这篇关于如何在 PDO fetchAll 中正确使用 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:mysql 中的 store_result() 和 get_result() 返回 false 下一篇:带有 pdo 的未定义类常量“MYSQL_ATTR_INIT_COMMAND&qu

                相关文章

                最新文章

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

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

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