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.

 

 

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.

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!

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.