absenm Posted April 18, 2012 Share Posted April 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261208-counting-total-different-elements-in-a-column/ Share on other sites More sharing options...
harristweed Posted April 19, 2012 Share Posted April 19, 2012 $cDiff = mysql_query("SELECT COUNT(DISTINCT cCode) AS number FROM onlinenow"); Quote Link to comment https://forums.phpfreaks.com/topic/261208-counting-total-different-elements-in-a-column/#findComment-1338688 Share on other sites More sharing options...
absenm Posted April 19, 2012 Author Share Posted April 19, 2012 still getting "Resource id #30" when I echo $cDiff; Quote Link to comment https://forums.phpfreaks.com/topic/261208-counting-total-different-elements-in-a-column/#findComment-1338692 Share on other sites More sharing options...
Barand Posted April 19, 2012 Share Posted April 19, 2012 You need to retreive the data from the returned result set ($cDiff). See mysql_fetch_row mysql_fetch_array mysql_fetch_assoc mysql_result Quote Link to comment https://forums.phpfreaks.com/topic/261208-counting-total-different-elements-in-a-column/#findComment-1338695 Share on other sites More sharing options...
absenm Posted April 19, 2012 Author Share Posted April 19, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/261208-counting-total-different-elements-in-a-column/#findComment-1338698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.