Jump to content

[SOLVED] Display most common entry in a mysql column.


kmutz22

Recommended Posts

The column is called 'nm' and i want to display its most common entry.

 

I tried this:

 
<?php
$sql="select
nm, 
count(nm) as total
from users
group by 
nm
order by
total desc";

$res=mysql_query($sql);
$obj=mysql_fetch_object($res);
$nm=$obj->nm;

print $nm;
?>

 

But it doesn't work and doesn't display anything. I really don't know what I'm doing or even if I'm on the right lines so any help would be greatly appreciated. Thanks.

 

 

Link to comment
Share on other sites

Add in some error checking:

 

$res=mysql_query($sql) or die("SQL WAS {$sql} <br /> Error was:" . mysql_error());
$obj=mysql_fetch_object($res);
$nm=$obj->nm;

 

And you will see why. As to why you have your sql spread out like that, I have no clue.

Link to comment
Share on other sites

Oh i just realised. The most common entry is nothing.

 

The sql looks like that because I was following a tutorial and they had done it like that, I don't really know why..

 

Thanks for everyone's help, can't believe I missed that one though!

 

 

 

Link to comment
Share on other sites

Hi, I know I clicked that this topic was solved, but I've been trying to do this for a while now. If the most common entry is nothing, how do i make it choose the most common from all the entries that are not empty?

 

thanks for any help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.