MrLols Posted August 8, 2011 Share Posted August 8, 2011 For a forum I have, I was wondering how to get the top 100 Posters I tried but kept getting errors & stuff so I decided to start fresh This is my counter of posts and topics : <?php $select = mysql_query("SELECT * FROM `posts` WHERE `post_by`='" . $pid . "' AND `post_first`='no';"); $posts = 0; while ($row = mysql_fetch_array($select)) { $posts += 1; } echo "Posts: " . $posts . PHP_EOL . nl2br(PHP_EOL) . PHP_EOL; $selectx = mysql_query("SELECT * FROM `topics` WHERE `topic_by`='" . $pid . "';"); $topics = 0; while ($trow = mysql_fetch_array($selectx)) { $topics += 1; } echo "Topics: " . $topics. PHP_EOL . nl2br(PHP_EOL) . PHP_EOL; } How would I make it count user_id's from the database , and make it list the top 100 with the most posts? My table for users is : user_id ; the ID of the user user_name ; name etc. MOD EDIT: code tags added. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 8, 2011 Share Posted August 8, 2011 Let's start by posting your errors. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 8, 2011 Share Posted August 8, 2011 Please post in the proper forum, and when posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 you are reading the entire table just to get a count... you don't need select * for that, you also don't need to loop through the results, you can just use mysql_num_rows or just count(). also, $select = mysql_query("SELECT * FROM `posts` WHERE `post_by`='" . $pid . "' AND `post_first`='no';"); does not require a semicolon in the statement (after `fist_post`='no') Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 8, 2011 Share Posted August 8, 2011 This query may work for what you want: select post_by, count(*) as total from posts p left join users u on(p.post_by = u.user_id) group by p.post_by desc limit 100 Quote Link to comment 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.