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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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?
Last edited by scarface222 on Thu Jul 23, 2009 2:00 pm, edited 1 time in total.
varma457
Forum Newbie
Posts: 23
Joined: Sat Jul 11, 2009 2:43 pm

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

Post by varma457 »

May be you can use group by with having function..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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]
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

You copied the query incorrectly. The syntax is not valid.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post by scarface222 »

k I figured out the syntax and the echo statement, sorry I was being an idiot lol thanks for your help guys
Post Reply