使用同一查询的多个PHP WHILE循环 [英] Multiple PHP WHILE loops using the same query
问题描述
$query1 = "SELECT * FROM idevaff_affiliates";
$affiliateID = mysql_query($query1) or die(mysql_error());
这是我上面的查询-我想将其用于两个WHILE循环
This is my query above - I would like to use it for two WHILE loops
第一个在标题部分-设置jQuery
The first one is in the header section - setting up jquery
while($row = mysql_fetch_assoc($affiliateID)){
}
第二个字符在主体的while循环中使用
The second is used in a while loop in the body
while($row = mysql_fetch_assoc($affiliateID)){
}
为什么我不能使它工作? -我确实可以使用它,但是必须使用两个不同的变量使用相同的SELECT信息进行两个查询.
Why can't I get it to work? - I did get it working but had to make two queries using the same SELECT info using two different variables.
推荐答案
调用mysql_fetch_assoc()
会检索下一行(即,您尚未检索的下一行).检索所有行后,它将返回false
.因此,一旦您完成了第一个循环,便已经检索了所有行,而每次返回的都是false
!
Calling mysql_fetch_assoc()
retrieves the next row (i.e., the next one you haven't already retrieved). Once you've retrieved all the rows, it returns false
. So, once you've gotten through that first loop, you have retrieved all the rows, and all you'll get back is false
every time!
如果您需要重复使用两次相同的数据,如何将它们全部放入一个数组中?
If you need to reuse the same data twice, how about putting it all in an array?
$rows = array();
while($row = mysql_fetch_assoc($affiliateID)){
$rows[] = $row;
}
现在,您可以随意遍历$rows
多次:
Now you can iterate through $rows
as many times as you like:
foreach($rows as $row) { ... }
这篇关于使用同一查询的多个PHP WHILE循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!