Siggles Posted June 20, 2008 Share Posted June 20, 2008 Hi, I am trying to get two things out of a MySQL database that I dont think I can do with one statement. If I put a SELECT statement within a php while loop (that is pulling data from a first SELECT statement) later echos do not appear. here is the code $result4 = mysql_query("SELECT predictions.username, `paidplayer`, COUNT(score), AVG(score), SUM(score), COUNT(id) AS Total_Score FROM predictions JOIN users ON predictions.username = users.username GROUP BY `username` ORDER BY Total_Score DESC "); $i=1; while($row = mysql_fetch_array($result4)) { $average=$row['AVG(score)']; $paidplayer=$row[paidplayer]; $result5 = mysql_query("SELECT SUM(score) FROM predictions WHERE score = '25'"); while($row = mysql_fetch_array($result5)) { $score25 = $row['SUM(score)']; if ($paidplayer== 2) { echo"<tr style=\"color: #FF0000;\">"; }else{ echo "<tr>"; } echo "<td>[". $i ."]</td>"; echo "<td>".$row[username]."</td>"; echo "<td>".$score25."</td>"; echo "<td>"; echo round($average, 2); echo "</td>"; echo "<td>".$row[Total_Score]."</td>"; echo "<td>".$row['COUNT(score)']."</td>"; echo "<td>".$row['SUM(score)']."</td>"; echo "</tr>"; $i++; }} Originally $result5 was not there and everythign worked fine but I need to get the SUM of a table where the table equals 25. Can you help? Link to comment https://forums.phpfreaks.com/topic/111101-solved-two-select-statements-what-am-i-doing-wrong/ Share on other sites More sharing options...
MadTechie Posted June 20, 2008 Share Posted June 20, 2008 Change the first $row to $row1 and the second $row to $row2 Ie <?php $result4 = mysql_query("SELECT predictions.username, `paidplayer`, COUNT(score), AVG(score), SUM(score), COUNT(id) AS Total_Score FROM predictions JOIN users ON predictions.username = users.username GROUP BY `username` ORDER BY Total_Score DESC "); $i=1; while($row1 = mysql_fetch_array($result4)) { $average=$row1['AVG(score)']; $paidplayer=$row1[paidplayer]; $result5 = mysql_query("SELECT SUM(score) FROM predictions WHERE score = '25'"); while($row2 = mysql_fetch_array($result5)) { $score25 = $row2['SUM(score)']; if ($paidplayer== 2) { echo"<tr style=\"color: #FF0000;\">"; }else{ echo "<tr>"; } echo "<td>[". $i ."]</td>"; echo "<td>".$row1['username']."</td>"; echo "<td>".$score25."</td>"; echo "<td>"; echo round($average, 2); echo "</td>"; echo "<td>".$row1[Total_Score]."</td>"; echo "<td>".$row1['COUNT(score)']."</td>"; echo "<td>".$row1['SUM(score)']."</td>"; echo "</tr>"; $i++; }} ?> Link to comment https://forums.phpfreaks.com/topic/111101-solved-two-select-statements-what-am-i-doing-wrong/#findComment-570198 Share on other sites More sharing options...
Siggles Posted June 20, 2008 Author Share Posted June 20, 2008 Simple as that :-) Thank you. Link to comment https://forums.phpfreaks.com/topic/111101-solved-two-select-statements-what-am-i-doing-wrong/#findComment-570240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.