Mysql Problem

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
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Mysql Problem

Post by cbrian »

Well, I'm working on a text-based game, and for a top ten list, I need to get the players with the most gold. Using the following code, I want to add the player's bank gold and gold together. But it ends up with this:

1 - cbrian (4,364,664,514g)
2 - ikki (8,581,934,390g)
3 - Ecko (8,581,935,340g)
... and the rest is fine.

Code:

Code: Select all

$query = mysql_query("SELECT * FROM users ORDER BY 'bank_gold'+'gold' DESC");
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

YOu could set it as a variable before you do the query

Code: Select all

$totalgold = $bank_gold + $gold;
And use $totalgold in your query.
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

That's what I thought at first, but this is in a loop, so it changes. I'm not sure if it would work.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Try this.

Code: Select all

$query = mysql_query("SELECT *, (bank_gold + gold) AS total_gold FROM users ORDER BY total_gold DESC");
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Ahhh, that worked great. Thanks a lot!
Post Reply