我测试循环嵌套工作时语句,以便:PHP嵌套While循环不mysql_fetch_assoc
$count1 = 0;
while ($count1 < 3) {
$count1++;
$count2 = 0;
echo "count1: ".$count1."<br />";
while ($count2 < 3) {
$count2++;
echo "count2: ".$count2."<br />";
}
}
这工作完全与结果(循环各三次):
count1: 1
count2: 1
count2: 2
count2: 3
count1: 2
count2: 1
count2: 2
count2: 3
count1: 3
count2: 1
count2: 2
count2: 3
然后我试图同使用mysql_fetch_assoc($ ContactsInterests是两行关联数组,并且$ LatestNeNe具有50行)的循环,即
$CI_count = 0;
while ($CI_Row = mysql_fetch_assoc($ContactsInterests)) { //loop thru interests
$CI_count++;
$LN_count = 0;
echo "CI_count: ".$CI_count."<br />";
while ($LN_Row = mysql_fetch_assoc($LatestNews)) { //loop thru news
$LN_count++;
echo "LN_count: ".$LN_count."<br />";
}
}
个
的结果是:
CI_count: 1
LN_count: 1
LN_count: 2
...
LN_count: 50
LN_count: 51
CI_count: 2
但它LN_count的第二次迭代?我不明白为什么LN_count没有第二次增加。
帮助赞赏。
mysql_data_seek($ result,0) – goat
顶部添加$ LN_Row = array(); 然后,在第一个while循环结束时添加重置($ LN_Row); – arijeet
@ redskins80'$ LN_Row'只包含由'mysql_fetch_assoc'传递的最后一个值......所以它不会填充'$ LN_Row',除非你像这样做'$ LN_Row [] = mysql_fetch_assoc' –