gpt4 book ai didi

php - 如何对两个循环使用一个查询

转载 作者:行者123 更新时间:2023-11-29 03:55:53 27 4
gpt4 key购买 nike

在这种情况下我该如何编写查询:这是查询

$gdQuery = $conn->query("blah blah ");

我想将上面的查询用于 while 循环两次。

  1. 是检索skillname(名字)
  2. 是检索学生姓名(student_fname)

这是正确的方法吗?如果不是,我该怎么做......?

<table>
<thead>
<th>Paricipant</th>
<?php while($gdData = $gdQuery->fetch_assoc()) { ?>
<th class="text-center"><?php echo $gdData['name']; ?></th> <?php } ?>
<th>Score</th>
</thead>
<tbody>
<tr>
<?php while($gdData = $gdQuery->fetch_assoc()) { ?>
<td><?php echo $gdData['student_fname']; ?></td> <?php } ?>
<td style="line-height:45px">Score</td>
</tbody>
</table>

My problem is when i write this the second while loop is not working

最佳答案

fetch_assoc 从查询结果中检索一行。您可以使用它,直到用完该结果集中的行。通过多次调用它,您正在接近结果集的末尾。这就是为什么第二个 while 循环无事可做的原因。热电阻http://php.net/manual/en/mysqli-result.fetch-assoc.php

在开始迭代之前使用 fetch_all 获取所有行并将它们存储在变量中。将该变量用于您的迭代。

或者,您可以尝试使用 data_seek 将结果集中的指针重置为在第一个 while 循环之后开始。 http://php.net/manual/en/mysqli-result.data-seek.php

关于php - 如何对两个循环使用一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197430/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com