Forum Moderators: coopster

Message Too Old, No Replies

Help with mysql count(*)

I need help counting the most frequently occuring record in a table

         

wonderbread

7:32 am on Jun 26, 2004 (gmt 0)

10+ Year Member



Hello everyone,

I've been tearing out my hair trying to get my latest project online. I have a user based site and some of the info is being stored in a mysql table where each record is identified by the user's username. I want to generate a list to be displayed online that shows the top ten users who have the most records. Basically just a list of the the most frequently occuring records in the table. Right now I am using the following SQL query:

$sql = "SELECT user, COUNT(*) as number FROM tablename GROUP BY user ORDER by number desc LIMIT 10";

$result = @mysql_query($sql, $connection) or die("Couldn't Execute Query");

I'm getting data back but am not sure if it is correct or exactly how to display it on the page. I typically use:

while ($row = mysql_fetch_array($result)) {}

but am not sure how to extract the count for each record using this. Any help you could give would be GREATLY appreciated!

Thanks!

Nick LAdd

ExpLarry

7:42 am on Jun 26, 2004 (gmt 0)

10+ Year Member



while ($row = mysql_fetch_array($result)) { 
print "$row[0]: $row[1]<br>";
}

or alternatively:

while ($row = mysql_fetch_array($result)) { 
print "$row['user']: $row['number']<br>";
}

I recommend the excellent PHP online documentation:
[php.net...]

(edit: correction)

jatar_k

9:36 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another option is to use the command line or phpmyadmin to view the exact output of the query.

It will help you get a better feel for what is returned and make it much easier to output.

wonderbread

9:49 pm on Jun 26, 2004 (gmt 0)

10+ Year Member



Got it working! Thanks!