<legend id='EBz2Y'><style id='EBz2Y'><dir id='EBz2Y'><q id='EBz2Y'></q></dir></style></legend>
<tfoot id='EBz2Y'></tfoot>

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

  1. <small id='EBz2Y'></small><noframes id='EBz2Y'>

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

    1. mysqli查询只返回第一行

      时间:2023-07-29

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

          • <tfoot id='pN0hU'></tfoot>
          • <small id='pN0hU'></small><noframes id='pN0hU'>

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

                <tbody id='pN0hU'></tbody>
                <i id='pN0hU'><tr id='pN0hU'><dt id='pN0hU'><q id='pN0hU'><span id='pN0hU'><b id='pN0hU'><form id='pN0hU'><ins id='pN0hU'></ins><ul id='pN0hU'></ul><sub id='pN0hU'></sub></form><legend id='pN0hU'></legend><bdo id='pN0hU'><pre id='pN0hU'><center id='pN0hU'></center></pre></bdo></b><th id='pN0hU'></th></span></q></dt></tr></i><div id='pN0hU'><tfoot id='pN0hU'></tfoot><dl id='pN0hU'><fieldset id='pN0hU'></fieldset></dl></div>
                本文介绍了mysqli查询只返回第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在从 mysql 迁移到 mysqli,但在查询中从数据库返回多于一行时遇到问题.

                I am migrating from mysql to mysqli, and I am having trouble returning more than one row from the database in a query.

                $db = new mysqli($hostname, $sql_us, $sql_us_pwd, $sql_db); // This is already connected
                
                function db_query($db, $query, $type = 'object') {
                
                    global $db;
                
                    $result = $db->query($query);
                
                    if ($type == 'assoc') {
                        while($row = $result->fetch_assoc()) {
                          return $row;
                        }
                    } else {    
                        while($row = $result->fetch_object()) {
                          return $row;
                        }   
                    }
                
                    mysqli_free_result($result);
                
                }
                
                
                
                $query = "SELECT * FROM `users`";
                $user = db_query($db, $query);
                print_r($user); // This is only returning the first row of results
                

                我显然是在尝试创建一个函数,我可以在其中查询数据库并以关联数组或对象的形式返回结果.我做错了什么?

                I'm obviously trying to make a function where I can query the database and either return the results in an associative array or as an object. What am I doing wrong?

                推荐答案

                使用此代码:

                $rows = array();
                if ($type == 'assoc') {
                    while($row = $result->fetch_assoc()) {
                      $rows[] = $row;
                    }
                } else {    
                    while($row = $result->fetch_object()) {
                      $rows[] = $row;
                    }   
                }
                return $rows;
                

                您在 while 内使用 return 并在第一次迭代后 return 终止 while 循环,这就是为什么您只得到一行.

                You are using the return inside the while and return terminates the while loop after first iteration that's why you are getting only one row.

                这篇关于mysqli查询只返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 PHP SESSION 变量存储 MySQL 查询结果 下一篇:未使用函数 password_verify 验证密码

                相关文章

                最新文章

              • <legend id='Ljxuy'><style id='Ljxuy'><dir id='Ljxuy'><q id='Ljxuy'></q></dir></style></legend>

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

                    <tfoot id='Ljxuy'></tfoot>
                      <bdo id='Ljxuy'></bdo><ul id='Ljxuy'></ul>