我似乎无法弄清楚为什么这个脚本会这样做,所以我需要有更多经验/更好的眼睛的人来告诉我问题是什么。我似乎无法在网上其他地方找到任何东西,但我的搜索字词可能是错误的。这组 While/Foreach 循环工作得很好,只是它会吐出同一行四次:
$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/>";
}
}
}
如果 name_records 表中的前两个条目是“Bob Ross, br01234”和“Alfred Hitchcock, ah43210”,那么上面的代码会输出:
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!
为什么是四个?为什么?!我尝试了几种不同的方法来修复它(对不起,我不记得我现在尝试了什么),但无济于事。