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

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

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

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

      1. mysqli 准备好的语句 num_rows 返回 0 而查询返回大于

        时间:2023-07-30
        • <bdo id='Ws2h1'></bdo><ul id='Ws2h1'></ul>

          <tfoot id='Ws2h1'></tfoot>

                  <tbody id='Ws2h1'></tbody>

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

                2. <i id='Ws2h1'><tr id='Ws2h1'><dt id='Ws2h1'><q id='Ws2h1'><span id='Ws2h1'><b id='Ws2h1'><form id='Ws2h1'><ins id='Ws2h1'></ins><ul id='Ws2h1'></ul><sub id='Ws2h1'></sub></form><legend id='Ws2h1'></legend><bdo id='Ws2h1'><pre id='Ws2h1'><center id='Ws2h1'></center></pre></bdo></b><th id='Ws2h1'></th></span></q></dt></tr></i><div id='Ws2h1'><tfoot id='Ws2h1'></tfoot><dl id='Ws2h1'><fieldset id='Ws2h1'></fieldset></dl></div>
                  <legend id='Ws2h1'><style id='Ws2h1'><dir id='Ws2h1'><q id='Ws2h1'></q></dir></style></legend>
                3. 本文介绍了mysqli 准备好的语句 num_rows 返回 0 而查询返回大于 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  对于实际存在的电子邮件,我有一个简单的准备声明:

                  I have a simple prepared statement for an email that actually exists:

                  $mysqli = new mysqli("localhost", "root", "", "test");
                  if (mysqli_connect_errno()) {
                      printf("Connect failed: %s
                  ", mysqli_connect_error());
                      exit();
                  }
                  
                  $sql = 'SELECT `email` FROM `users` WHERE `email` = ?';
                  $email = 'example@hotmail.com';
                  
                  if ($stmt = $mysqli->prepare($sql)) {
                      $stmt->bind_param('s', $email);
                      $stmt->execute();
                  
                      if ($stmt->num_rows) {
                          echo 'hello';
                      }
                  
                      echo 'No user';
                  }
                  

                  结果:在应该回显 hello

                  我在控制台中运行了相同的查询,并使用与上述相同的电子邮件得到了结果.

                  I ran the same query in the console and got a result using same email as above.

                  我也使用简单的 mysqli 查询进行了测试:

                  I tested using a simple mysqli query as well:

                  if ($result = $mysqli->query("SELECT email FROM users WHERE email = 'example@hotmail.com'")) {
                      echo 'hello';
                  
                  }
                  

                  结果:我所期望的hello

                  还有 $resultnum_rows 是 1.

                  Also $result's num_rows is 1.

                  为什么准备好的语句的num_row不大于0?

                  Why is the prepared statment's num_row not greater than 0?

                  推荐答案

                  当您通过 mysqli 执行语句时,结果实际上并不在 PHP 中,直到您获取它们——结果由数据库引擎保存.所以mysqli_stmt对象无法知道执行后立即有多少结果.

                  When you execute a statement through mysqli, the results are not actually in PHP until you fetch them -- the results are held by the DB engine. So the mysqli_stmt object has no way to know how many results there are immediately after execution.

                  像这样修改你的代码:

                  $stmt->execute();
                  $stmt->store_result(); // pull results into PHP memory
                  
                  // now you can check $stmt->num_rows;
                  

                  查看手册

                  这不适用于您的特定示例,但如果您的结果集很大,$stmt->store_result() 将消耗大量内存.在这种情况下,如果您只关心是否至少返回了一个结果,请不要存储结果;相反,只需检查结果元数据是否不为空:

                  This doesn't apply to your particular example, but if your result set is large, $stmt->store_result() will consume a lot of memory. In this case, if all you care about is figuring out whether at least one result was returned, don't store results; instead, just check whether the result metadata is not null:

                  $stmt->execute();
                  $hasResult = $stmt->result_metadata ? true : false;
                  

                  查看手册

                  这篇关于mysqli 准备好的语句 num_rows 返回 0 而查询返回大于 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:致命错误:调用未定义的方法 mysqli_stmt::get_result 下一篇:PHP-MySQL 或 MySQLi 中哪个最快?

                  相关文章

                  最新文章

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

                4. <tfoot id='saEiV'></tfoot>
                  • <bdo id='saEiV'></bdo><ul id='saEiV'></ul>

                5. <small id='saEiV'></small><noframes id='saEiV'>

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