Jump to content

Counting total different elements in a column


absenm

Recommended Posts

I've been searching around for this for a while and nothing seems to be working properly.

 

On my website I track who is online at the time (within the past 15 minutes).  My database contains important information like ip addresses and page views during the visit.  It also contains a column for two digit country codes (US, NL, CA, GB, etc.)  It works great.  But I'd like to display to my visitors how many countries people are visiting from at the time.  I might have ten people from the US, three from England, and one from Canada.  I'd like it to say we have visitors from 3 countries.  My hope this could be accomplished with a simple query.  Here's the code I have ended up with.  But it's not working.

 

Each visitor has one row in the table.

 

"cCode" is the column name and "onlinenow" is the table name.  I would have changed the names in my code below but I wanted to post my code exactly as is.

 

$cDiff = mysql_query("SELECT cCode, COUNT(cCode) AS number FROM onlinenow");

 

I also tried....

 

$cDiff = mysql_query("SELECT cCode, COUNT(DISTINCT cCode) AS number FROM onlinenow");

 

It results in returning "Resource id#30" for the value of cDiff.  What am I doing wrong.  I am self taught and please forgive me if I didn't use the correct lingo.

 

Thank You In Advance.

Oh gee.... I should have known/noticed that.  Arggg...  That's what happens when your self taught.  Got it to work now.  Thanks everyone for your help.  In the interest of completeness, here is the working code I came up with.

 

$onlineData = mysql_query("SELECT COUNT(DISTINCT cCode) AS number FROM onlinenow");
$row = mysql_fetch_array($onlineData);
$cDiff = $row[0];

 

Thanks Again!

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.