duandong9195 2018-11-03 15:35
浏览 28
已采纳

如何使用PHP显示两次相同的MySQL查询?

I selected values from my table, and wanted to display them two separate times. In the first instance, I wanted to display all cells in a table, and this works. In the second instance, I wanted to make a drop down from one of the columns. Only the first one works; how can I get both to work? I also need all of the rows in the table to display, which is why I thought a while loop was a good idea. But please correct me if I’m wrong in my thinking. Thanks.

<!DOCTYPE html>
<?php

$host = "localhost";
$username = "redacted";
$password = "redacted";
$db = "redacted";

$connection = mysqli_connect($host, $username, $password, $db);

if (mysqli_connect_errno()) {
  echo "Connection error";
}

$query = "SELECT * FROM main";

$result = $connection->query($query);

?>

<html>
  <head>
    <title>Test Page</title>
  </head>
  <body>
    <h1>Test Main Table Display</h1>
    <table>
      <?php
      // first instance
      while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['user'] . "</td>";
        echo "<td>" . $row['date'] . "</td>";
        echo "<td>" . $row['count'] . "</td>";
        echo "<td>" . $row['action'] . "</td>";
        echo "<td><button>Expand</button></td>";
        echo "</tr>";
      }
      ?>
    </table>
    <h1>Test User Entry Appendment</h1>
    <form>
      <label>Select a user, or add a new user.</label><br>
      <select>
        <?php
        // second instance
        while($row = mysqli_fetch_array($result)) {
          echo "<option>" . $row['user'] . "</option>";
        }
        ?>
      </select>
    </form>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongtao9887 2018-11-03 15:40
    关注

    add this line before second loop set the pointer back to the beginning

    mysqli_data_seek($result, 0);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址