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

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

      1. Mysqli 查询不能工作两次

        时间:2023-07-30

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

        1. <tfoot id='sTHOW'></tfoot>

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

              <tbody id='sTHOW'></tbody>
            <legend id='sTHOW'><style id='sTHOW'><dir id='sTHOW'><q id='sTHOW'></q></dir></style></legend>

                  <bdo id='sTHOW'></bdo><ul id='sTHOW'></ul>
                  本文介绍了Mysqli 查询不能工作两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我无法同时使用 Mysqli 查询.如果我在 html 中注释掉一个函数,另一个函数就会正确执行,反之亦然.

                  function all_posts() {require_once 'database.inc.php';$mysqli = mysqli_connect($host, $username, $password, $database);$query = mysqli_query($mysqli, "SELECT variable_name, post_name, post_date, post_display FROM blog_posts ORDER BY id DESC LIMIT 5");如果(!$查询)回声 mysqli_error();而 ($results = mysqli_fetch_assoc($query)) {$post_name = $results['post_name'];$post_date = $results['post_date'];$post_display = $results['post_display'];$variable_name = $results['variable_name'];echo "";echo "

                  ";echo "

                  ";echo "<h2>{$post_name}</h2>";echo "<h3>{$post_date}</h3>";回声</div>";echo "<p>{$post_display}</p>";回声</div>";回声</a>";}mysqli_free_result();}函数 all_sidebar_posts() {require_once 'database.inc.php';$mysqli = mysqli_connect($host, $username, $password, $database);$query = mysqli_query($mysqli, "SELECT variable_name, post_name FROM blog_posts ORDER BY id DESC LIMIT 5");而 ($results = mysqli_fetch_assoc($query)) {$post_name = $results['post_name'];$variable_name = $results['variable_name'];echo "<li><a href='posts.php?post=$variable_name'>$post_name</a></li>";}mysqli_free_result();}

                  这是我要输出到的 html.

                    <?php all_sidebar_posts();?>

                  <div class="content_container"><?php all_posts();?>

                  我尝试过使用 mysqli_data_seek(); 但没有成功.也许我没有正确使用它?我浏览了许多问题并找到了类似的问题,但我都尝试过,但都无济于事.我是编程新手,所以我可能会忽略一些基本的东西.谢谢大家的帮助!

                  解决方案

                  你做错了.
                  切勿将您的数据操作代码与演示代码混合在一起.

                  首先,将帖子放入数组:

                  require_once 'database.inc.php';mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);$mysqli = mysqli_connect($host, $username, $password, $database);$sql = "SELECT variable_name, post_name, post_date, post_displayFROM blog_posts ORDER BY id DESC LIMIT 5"$result = mysqli_query($mysqli, $sql);$data = 数组();而 ($row = mysqli_fetch_assoc($result)) {$data[] = $row;}

                  然后使用这个 $data 数组在您需要的任何时间显示帖子,只需使用 foreach()

                  I cannot get my Mysqli queries to both work. If I comment out one function in my html, the other function is properly executed and vice versa.

                  function all_posts() {
                      require_once 'database.inc.php';
                      $mysqli = mysqli_connect($host, $username, $password, $database);
                      $query = mysqli_query($mysqli, "SELECT variable_name, post_name, post_date, post_display FROM blog_posts ORDER BY id DESC LIMIT 5");
                  
                      if (!$query)
                          echo mysqli_error();
                  
                      while ($results = mysqli_fetch_assoc($query)) {
                  
                          $post_name = $results['post_name'];
                          $post_date = $results['post_date'];
                          $post_display = $results['post_display'];
                          $variable_name = $results['variable_name'];
                  
                          echo "<a href='posts.php?post={$variable_name}'>";
                          echo "<div class='entry'>";
                          echo "<div class='entry_header'>";
                          echo "<h2>{$post_name}</h2>";
                          echo "<h3>{$post_date}</h3>";
                          echo "</div>";
                          echo "<p>{$post_display}</p>";
                          echo "</div>";
                          echo "</a>";
                      }
                  
                      mysqli_free_result();
                  }
                  
                  function all_sidebar_posts() {
                  
                      require_once 'database.inc.php';
                      $mysqli = mysqli_connect($host, $username, $password, $database);
                      $query = mysqli_query($mysqli, "SELECT variable_name, post_name FROM blog_posts ORDER BY id DESC LIMIT 5");
                  
                      while ($results = mysqli_fetch_assoc($query)) {
                  
                          $post_name = $results['post_name'];
                          $variable_name = $results['variable_name'];
                          echo "<li><a href='posts.php?post=$variable_name'>$post_name</a></li>";
                      }
                  
                      mysqli_free_result();
                  }
                  

                  Here is the html that I am outputting to.

                  <ul>
                      <?php all_sidebar_posts(); ?>
                  </ul>
                  </div>
                  <div class="content_container">
                      <?php all_posts(); ?>
                  </div>
                  

                  I have tried using mysqli_data_seek(); but haven't had luck. Perhaps I am not using it right? I have browsed many questions and found similar ones but I have tried them all to no avail. I am new to programming so I may be overlooking something basic. Thank you all for the help!

                  解决方案

                  You are doing it wrong way.
                  Never mix your data manipulation code with presentation code.

                  First, get the posts into array:

                  require_once 'database.inc.php';
                  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
                  $mysqli = mysqli_connect($host, $username, $password, $database);
                  
                  $sql = "SELECT variable_name, post_name, post_date, post_display 
                          FROM blog_posts ORDER BY id DESC LIMIT 5"
                  $result = mysqli_query($mysqli, $sql);
                  $data = array();
                  while ($row = mysqli_fetch_assoc($result)) {
                      $data[] = $row;
                  }
                  

                  and then use this $data array to display posts any times you need, simply using foreach()

                  这篇关于Mysqli 查询不能工作两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:php 5.2.6 的 mysqli_stmt_get_result 替代方案 下一篇:如何使用 MySQLi 在 PHP 中获取数据?

                  相关文章

                  最新文章

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

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