While/foreach Loops Returning Four of the Same Row

守給你的承諾、 提交于 2019-12-25 00:36:15

问题


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.


回答1:


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/>";
         }
     //}



回答2:


The problem is that you're iterating through the columns. Remove the foreach loop and you should be good.




回答3:


You can simplify your code by directly querying what you need from database and iterating on the simple result.

SELECT name, username, sum(gradpoint.totalseconds) as total FROM name_records, gradpoint where name_records = gradpoint.username group by gradpoint.username



回答4:


You could acheve what you wish with some sort of join and group. Somewthing like:

   $squery = "SELECT nr.username, nr.name, SUM(g.totalseconds) as totsec FROM name_records nr, gradpoint g
WHERE g.username=nr.username
GROUP BY nr.username,nr.name";

Might work




回答5:


$query = "
SELECT u.*, (SUM(g.totalseconds) / 60) as sum
FROM name_records u
JOIN gradpoint g
ON  u.username = g.username
GROUP BY u.username;
";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    echo $row['name'] . " has worked for " . $row['sum'] . " minutes today! Good jorb!<br/>";
}

Also include divide by 60.



来源:https://stackoverflow.com/questions/14988879/while-foreach-loops-returning-four-of-the-same-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!