Ashikaa Posted October 14, 2008 Share Posted October 14, 2008 I have the following table in database Table's name: Record ID Name Score 1 A 10 2 B 20 3 C 30 4 D 40 5 E 50 I would like to diplay top 3 score in PHP like this 5 E 50 4 D 40 3 C 30 Thank for any help and reply! Ash Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/ Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Try this (not tested): $result = mysql_query("SELECT * FROM table_name ORDER BY Score LIMIT 3"); while($row = mysql_fetch_array($result)) { //HTML formatting (tables etc...) echo $row['ID'] . " " . $row['Name'] . " " . $row['Score']; echo " "; } Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665298 Share on other sites More sharing options...
dennismonsewicz Posted October 14, 2008 Share Posted October 14, 2008 you could change your SQL statement to look like this $result = mysql_query("SELECT * FROM table_name ORDER BY Score DESC LIMIT 3"); The DESC sorts the query from the last record to the first... now this is only a quick fix as long as your biggest record is the last record in the database Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665313 Share on other sites More sharing options...
discomatt Posted October 14, 2008 Share Posted October 14, 2008 The DESC sorts the query from the last record to the first... now this is only a quick fix as long as your biggest record is the last record in the database Incorrect. ORDER BY `columnName` DESC will order by that column name in descending order ( z-a.. highest to lowest... ect ) Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665317 Share on other sites More sharing options...
dennismonsewicz Posted October 14, 2008 Share Posted October 14, 2008 thats right sorry... I mixed myself up there. I thought about that after I posted it just didn't get a chance to clean it up. Thanks Disco Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665320 Share on other sites More sharing options...
revraz Posted October 14, 2008 Share Posted October 14, 2008 Ascending 10 20 30 Decending 30 20 10 For the OP's example, he wants the top 3, which would require a DESC option since default is ASC. Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665321 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 For the OP's example, he wants the top 3, which would require a DESC option since default is ASC. Yes, you're right. Sorry Ashkiaa you should change the query to what dennis proposed. Quote Link to comment https://forums.phpfreaks.com/topic/128405-display-highest-value-from-database-question/#findComment-665326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.