Kingy Posted January 22, 2008 Share Posted January 22, 2008 mysql>SET @rank:=0; mysql>SELECT rank, user FROM (SELECT @rank:=@rank+1 AS rank, id, user, written FROM stats ORDER BY written DESC) AS foo WHERE user='Kingy'; +------+-------+ | rank | user | +------+-------+ | 4 | Kingy | +------+-------+ that works perfectly for what i want to do, but as u can see im running that from the mysql command prompt, how would i get that exact output, using php? Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/ Share on other sites More sharing options...
btherl Posted January 22, 2008 Share Posted January 22, 2008 The simplest way would be to fetch the results in order, and add the ranks in php. I'm not sure how to do it in pure mysql. Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445827 Share on other sites More sharing options...
Kingy Posted January 22, 2008 Author Share Posted January 22, 2008 yeah im quite confused about how to do this in php.. i was about to try and write some code, but i actually have no idea :S anyone? Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445830 Share on other sites More sharing options...
awpti Posted January 22, 2008 Share Posted January 22, 2008 http://www.php.net/mysql/ Read up and enjoy. There are some pretty simple examples there. Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445863 Share on other sites More sharing options...
Kingy Posted January 22, 2008 Author Share Posted January 22, 2008 <?php $query = "select * from stats order by written desc"; $query = mysql_query($query); $rank = 0; while($row = mysql_fetch_array($query)){ $rank++; if ($row['user'] == "Kingy"){ echo "You are ranked $rank; $rank = 0; break; } } ?> well i have that and its works fine, what i want now is to have echo "You are ranked $rank out of $total"; how would i go about getting the total, i'd say i'd use some sort of count? Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445899 Share on other sites More sharing options...
mmarif4u Posted January 22, 2008 Share Posted January 22, 2008 U can use count and also u have to use where if for some specific record you want to do counting. Or: $num=mysql_num_rows($query); also can use. Then use if statement and give condition like: if($num > 10) { do something }else { blah blah } Am i right to get your idea. Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445913 Share on other sites More sharing options...
Kingy Posted January 22, 2008 Author Share Posted January 22, 2008 am i able to just echo $num to find out the total of rows it finds or not? Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445993 Share on other sites More sharing options...
btherl Posted January 25, 2008 Share Posted January 25, 2008 mysql_num_rows($query) will give you the number of rows. BTW, I suggest you don't use $query for both the query itself and the result, as they are different things. Better to do like this: $query = "select * from stats order by written desc"; $result = mysql_query($query); if (!$result) die("Error in $query: " . mysql_error()); print "There were " . mysql_num_rows($result) . " rows returnd\n"; $rank = 0; while($row = mysql_fetch_array($result)){ $rank++; if ($row['user'] == "Kingy"){ echo "You are ranked $rank; $rank = 0; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-448441 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.