Page 1 of 1

(solved) Taking the best and most of columns.

Posted: Wed Apr 30, 2008 10:28 am
by Mightywayne
Hey, remember me? :P Anyway, I'm stuck here on making a "world stats" page for my game. I want to tally up all the money in it, and display it as a variable. I could easily do this with a total pain-in-the-ass loop, but I'm quite certain there's a better way to do it with SQL.

Code: Select all

 
$allmoney = mysql_query("SELECT SUM(money) FROM xx")or die(mysql_errror());
      $allmoney = mysql_fetch_array($allmoney);
      $allmoney = $allmoney['money'];
      
      $hilevelmon = mysql_query("SELECT max(level) FROM xx LIMIT 0, 1");
      $hilevelmon = mysql_fetch_array($hilevelmon);
      $hilevelmon = $hilevelmon['level'];
 
The first is for all money, and the second for the highest-leveled monster in the game. However, I just can't figure out how to display them, I guess. :/ What to do?

EDIT: I figured it out. So odd, I guess I didn't expect it to work like this:

$allmoney = $allmoney['money']; should be " $allmoney = $allmoney['SUM(money)'];"

Re: (solved) Taking the best and most of columns.

Posted: Fri May 02, 2008 1:25 pm
by RobertGonzalez
You can always alias them by doing:
[sql]SELECT SUM(money) AS sum_money FROM xx[/sql]

Then the array key in the result becomes 'sum_money'.