while / foreach循环返回同一行中的四个 [英] While/foreach Loops Returning Four of the Same Row
问题描述
$ b $ pre $ $ $ $ $ $ $ $用户名,名称从name_records;
$ sresult = mysql_query($ squery);
while($ srow = mysql_fetch_array($ sresult)){
foreach($ srow as $ scell){
$ username = $ srow ['username'];
$ name = $ srow ['name'];
$ queryi =SELECT SUM(totalseconds)FROM gradpoint WHERE username ='$ username';
$ resulti = mysql_query($ queryi)或die(mysql_error());
while($ rowi = mysql_fetch_array($ resulti)){
$ total = $ rowi ['SUM(totalseconds)'];
$分钟= $ total / 60;
echo $ name。已经为。分钟。分钟今天!好jorb!< p />;
如果前两个条目在name_records表是Bob Ross,br01234和Alfred Hitchcock,ah43210,那么上面的代码就会吐出来:
Bob Ross今天工作了342分钟!好jorb!
鲍勃罗斯今天工作了342分钟!好jorb!
鲍勃罗斯今天工作了342分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
为什么是四个?为什么?!我尝试了几种不同的方法来解决这个问题(对不起,我不记得我现在尝试了什么),但是没有用。
解决方案我认为这个问题来自foreach:
$ $ $ $ $ s $ $ $ $ $ b $ username = $ srow ['username'];
$ name = $ srow ['name'];
$ queryi =SELECT SUM(totalseconds)FROM gradpoint WHERE username ='$ username';
$ resulti = mysql_query($ queryi)或die(mysql_error());
while($ rowi = mysql_fetch_array($ resulti)){
$ total = $ rowi ['SUM(totalseconds)'];
$分钟= $ total / 60;
echo $ name。已经为。分钟。分钟今天!好jorb!< p />;
//
I can't seem to figure out why this script is doing this, so I need someone with more experience/better eyes to tell me what the issue is. I can't seem to find anything elsewhere online, but I may be wording my search terms wrong. This set of While/Foreach loops works just fine except that it spits out the same row four times:
$squery = "SELECT username, name FROM name_records";
$sresult = mysql_query($squery);
while($srow = mysql_fetch_array($sresult)) {
foreach ($srow as $scell) {
$username = $srow['username'];
$name = $srow['name'];
$queryi = "SELECT SUM(totalseconds) FROM gradpoint WHERE username = '$username'";
$resulti = mysql_query($queryi) or die(mysql_error());
while($rowi = mysql_fetch_array($resulti)){
$total = $rowi['SUM(totalseconds)'];
$minutes = $total / 60;
echo $name ." has worked for " .$minutes ." minutes today! Good jorb!<p/>";
}
}
}
If the first two entries in the name_records table were "Bob Ross, br01234" and "Alfred Hitchcock, ah43210", then the above code would spit out this:
Bob Ross has worked for 342 minutes today! Good jorb!
Bob Ross has worked for 342 minutes today! Good jorb!
Bob Ross has worked for 342 minutes today! Good jorb!
Bob Ross has worked for 342 minutes today! Good jorb!
Alfred Hitchcock has worked for 187 minutes today! Good jorb!
Alfred Hitchcock has worked for 187 minutes today! Good jorb!
Alfred Hitchcock has worked for 187 minutes today! Good jorb!
Alfred Hitchcock has worked for 187 minutes today! Good jorb!
Why four? Why?! I tried a few different ways to fix it (sorry, I don't remember what I tried now), but to no avail.
I think the issue came from the foreach:
//foreach ($srow as $scell) {
$username = $srow['username'];
$name = $srow['name'];
$queryi = "SELECT SUM(totalseconds) FROM gradpoint WHERE username = '$username'";
$resulti = mysql_query($queryi) or die(mysql_error());
while($rowi = mysql_fetch_array($resulti)){
$total = $rowi['SUM(totalseconds)'];
$minutes = $total / 60;
echo $name ." has worked for " .$minutes ." minutes today! Good jorb!<p/>";
}
//}
这篇关于while / foreach循环返回同一行中的四个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!