Page 1 of 1

help with difficult php/mysql syntax...advice appreciated

Posted: Sun Jul 12, 2009 1:06 am
by scarface222
Hey guys I am trying to do a mysql select in php and it is probably easy for some but I am not sure how to do it. I want to create a select statement to find out which user is entered in my table the most. How do I approach this?

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Sun Jul 12, 2009 1:51 am
by varma457
May be you can use group by with having function..

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Sun Jul 12, 2009 2:50 am
by onion2k
You don't need HAVING. Just COUNT() and GROUP BY. Eg

[sql]SELECT `value`, COUNT(`value`) AS total FROM `table` GROUP BY `value` ORDER BY total DESC LIMIT 1[/sql]

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Thu Jul 23, 2009 1:59 pm
by scarface222
Thanks for the reply. I wanted to see which user was entered in the database the most so I tried your suggestion

Code: Select all

$query="SELECT 'user', COUNT('user') AS total FROM 'data' GROUP BY 'user' ORDER BY total DESC LIMIT";
            $result=mysql_query($query) or die('Error, select query failed');
I just get error select query failed. Does anyone else know how to do this and how do you echo the result (the most entered user) when finished?

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Thu Jul 23, 2009 5:43 pm
by scarface222
Also I almost forgot I just want to display 3 entries anyone have an idea I can't find an answer for this. Thanks in advance for any help...

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Thu Jul 23, 2009 6:24 pm
by Benjamin
You copied the query incorrectly. The syntax is not valid.

Re: help with difficult php/mysql syntax...advice appreciated

Posted: Thu Jul 23, 2009 9:08 pm
by scarface222
k I figured out the syntax and the echo statement, sorry I was being an idiot lol thanks for your help guys