如何在两个循环中使用一个查询 [英] how to use one query for two loops

查看:101
本文介绍了如何在两个循环中使用一个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can i write query in this situation: this is the query

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

我想将上述查询用于两次的while循环.

i want to use the above query for while loop that is two time.

  1. 是要检索技能名称(名称)
  2. 是要检索的学生姓名(student_fname)

如果不是的话,这是正确的方法吗?.

is it a correct approach if not how can i do that..?

<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>

我的问题是当我在第二个while循环中编写此代码时不起作用

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

推荐答案

fetch_assoc从查询结果中检索一行.您可以使用它,直到该结果集中的行用完为止.通过多次调用它,您正在走向结果集的结尾.这就是为什么第二次while循环不执行任何操作的原因. RTD http://php.net/manual/en/mysqli-result .fetch-assoc.php

fetch_assoc retrieves one row from query result. You can use it until you run out of rows in that result set. By calling it multiple times you are moving towards the end of result set. That is reason why there is nothing to do for second while loop. RTD http://php.net/manual/en/mysqli-result.fetch-assoc.php

使用fetch_all获取所有行,然后再开始迭代并将它们存储在变量中.使用该变量进行迭代.

Use fetch_all to get all rows before you start iterating and store them in variable. Use that variable for your iterations.

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

Alternatively you may try to use data_seek to reset pointer in result set to beginning after first while loop. http://php.net/manual/en/mysqli-result.data-seek.php

这篇关于如何在两个循环中使用一个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆