Jump to content

[SOLVED] Two SELECT statements, what am I doing wrong?


Siggles

Recommended Posts

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?

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++;
  	}}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.