0

I'm trying to make a top users of my website, but the problem that I'm having is if one or more users have the same amount of won games only the first result appears on the website.

Im using this code to search in the database:

$rs1 = mysql_query(SELECT won,steamid,name,avatar,games 
   FROM `users` 
   WHERE won <> 0 
   GROUP BY won DESC LIMIT 18); 
while($row = mysql_fetch_array($rs1))
{ //AND HTML CODE HERE...}

Can someone help me with this? I want to show all the users for example if they have the same number of won games, for example it would show like:

RANK - USER - WON
1    - NAME - 12
2    - NAME - 8
3    - NAME - 8
4    - NAME - 4

BTW I know that I should not use mysql_query but I can't do it another way.

1
  • this is just the obligatory comment informing you that you shouldn't use mysql_* functions anymore. they are deprecated and, in php7, removed. use mysqli_* or PDO instead. Commented Jul 14, 2016 at 12:57

3 Answers 3

1

I don't think you need the group by clause, but if you do, please consider using all the projection fields on group by. Although mysql allows you to group by different fields, it's not a good practice to do so.

Sign up to request clarification or add additional context in comments.

Comments

0

Seems to me that your GROUP BY should be an ORDER BY

rs1 = mysql_query(SELECT won,steamid,name,avatar,games 
               FROM `users` 
               WHERE won <> 0 
               ORDER BY won DESC LIMIT 18); 

If that does not work please share the table structure

Comments

0

Add name in to Group by clause

SELECT won,steamid,name,avatar,games FROM users WHERE won <> 0 
GROUP BY won,name order by won,name DESC LIMIT 18

4 Comments

Well there is just one problem solved... now im getting all the users bet they are not ordered from the one who have more won games
Excuse my ignorance but dont you need a ORDER BY something DESC
@RiggsFolly thanks i just replaced the GROUP by ORDER and it's done.
@RiggsFolly Updated query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.