Sam1 Posted January 14, 2010 Share Posted January 14, 2010 Hello I want to learn how to make a script, that reads 2 tabels to find and print top 10 users Table1: postname - userid Tabel2: username - id ( userid = id ) the script needs to count and print top 10 users with most posts #1 userA 1000 Posts #2 userB 900 Posts Thank you Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/ Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 SELECT *, COUNT(userid) as `cnt` FROM table1, table2 WHERE userid = id GROUP BY userid ORDER BY cnt DESC Something along those lines should do it Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-994968 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 You should avoid calculation as much as you can. Instead add a post_count to the users table and then use ORDER BY post_count DESC Increment the post_count on each new post the user makes. This is also usefull for other business logic for example if your post count should not increase if you post in a certain forum. Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-994972 Share on other sites More sharing options...
ToonMariner Posted January 14, 2010 Share Posted January 14, 2010 MYSQL is perectly happy performing calculations - and why bother storing such trivial data as post count? $qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10"; Something like that should do the trick... Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-994979 Share on other sites More sharing options...
Sam1 Posted January 14, 2010 Author Share Posted January 14, 2010 WOW...you guys are amazing....thank you so much I'm Beginner so please Be patient with me how to print(echo) query result? $qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10"; Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-994985 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 and why bother storing such trivial data as post count? This is also usefull for other business logic for example if your post count should not increase if you post in a certain forum. Being one reason, second reason would be because most likely it's going to appear as a widget in the left or right nav of the website and when you have many visitors (and thus many posts) your using your database intensively (calculating the total post_count over and over) to provide you with data that is trivial to your website. Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-994988 Share on other sites More sharing options...
Sam1 Posted January 14, 2010 Author Share Posted January 14, 2010 I tried get_result ("select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10"); echo $query; did not work Warning: mysql_fetch_assoc() expects parameter 1 to be resource Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-995067 Share on other sites More sharing options...
Sam1 Posted January 15, 2010 Author Share Posted January 15, 2010 WOW...you guys are amazing....thank you so much I'm Beginner so please Be patient with me how to print(echo) query result? $qry = "select DISTINCT(`table2`.`username`) AS `username`, COUNT(`table1`,`userid`) AS `postcount` FROM `table2` LEFT JOIN `table1` ON `table1`.`userid` = `table2`.`userid` GROUP BY `table2`.`userid` ORDER BY `postcount` DESC LIMIT 0,10"; Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-995368 Share on other sites More sharing options...
Sam1 Posted January 15, 2010 Author Share Posted January 15, 2010 JAY6390 I tried $qry = "SELECT *, COUNT(userid) as `cnt` FROM table1, table2 WHERE userid = id GROUP BY userid ORDER BY cnt DESC"; $result = mysql_query($qry) or die(mysql_error()); echo $result; and it prints "Resource id #68 " !!! what is my mistake? Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-995385 Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 Your mistake is that mysql_query returns a result resource, you can't simply echo them. You'll need to turn your result resource into an array with mysql_fetch_assoc or similar, from there you can get to your data via the arrays indexes. Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-995388 Share on other sites More sharing options...
Sam1 Posted January 15, 2010 Author Share Posted January 15, 2010 Your mistake is that mysql_query returns a result resource, you can't simply echo them. You'll need to turn your result resource into an array with mysql_fetch_assoc or similar, from there you can get to your data via the arrays indexes. thank you for your help *** like this? $qry = "SELECT *, COUNT(userid) as `cnt` FROM table1, table2 WHERE userid = id GROUP BY userid ORDER BY cnt DESC LIMIT 0,10";"; $result = mysql_query($qry) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["cnt"]; } mysql_free_result($result); and how can i make the result into 2 columns , because now it like this 3333225614848152238156134101321073301129436232119 thank you Quote Link to comment https://forums.phpfreaks.com/topic/188464-top-10-users/#findComment-995401 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.