Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.