Jump to content

[SOLVED] quick php fix


Jiraiya

Recommended Posts

ok my script is supposed to add two variables up and then display them after grouping them. the script half works in the sense that it only totals and groups one variable or the other variable not both im not sure whats wrong

 

 

 

$query = "SELECT village, SUM(skill) FROM users GROUP BY village"; 
$query = "SELECT village, SUM(health) FROM users GROUP BY village";	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Village  ". $row['village']. " total power: ". $row['SUM(skill)'];
echo "Village  ". $row['village']. " total health: ". $row['SUM(health)'];
echo "<br />";
}

Link to comment
https://forums.phpfreaks.com/topic/136243-solved-quick-php-fix/
Share on other sites

Here:

$query = "SELECT village, SUM(skill) FROM users GROUP BY village";
$query = "SELECT village, SUM(health) FROM users GROUP BY village";   
$result = mysql_query($query) or die(mysql_error());

Only your last query will be run.

 

Your query should be

$query = "SELECT village, SUM(skill), SUM(health) FROM users GROUP BY village";   
$result = mysql_query($query) or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/136243-solved-quick-php-fix/#findComment-710698
Share on other sites

what would that fix if it only displayed one result?

I have only merged your two queries together. As before only the last query was being run:

$query = "SELECT village, SUM(health) FROM users GROUP BY village";   

 

The first query:

$query = "SELECT village, SUM(skill) FROM users GROUP BY village";

Is not being run at all, as the last query above is overriding it.

 

This is why I have merged your two queries.

Link to comment
https://forums.phpfreaks.com/topic/136243-solved-quick-php-fix/#findComment-710706
Share on other sites

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.