(solved) Taking the best and most of columns.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

(solved) Taking the best and most of columns.

Post 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)'];"
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post 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'.
Post Reply