I have created my query and want to extract each row from the table by using But I get this error Any ideas on how to fetch the data?
Got it working I'm such a novice. I had DISTINCT(Referrer) instead of DISTINCT Referrer. Mixing my COUNT with my DISTINCTs.
No, the proper SQL syntax is the first one. It will return one column of all the unique values in the column referrer in the table referrer.
$resulta = mysql_query("SELECT DISTINCT(*)) FROM Refferrer"); This line has more close brackets than open brackets. You also need to specify what fields you want to be distinct. You're probably getting an empty result set back at present. $resulta = mysql_query("SELECT DISTINCT <field_name, [more_fields]> FROM Refferrer"); Would be better.
Thanks Draculas. MY next problem is how to get the count of these Referrals and display them in descending order. Sometimes I think I just like causing myself problems.
Try SELECT DISTINCT(referrer) as referrer,COUNT(referrer) as referrerCount from referrer GROUP BY referrer ORDER BY COUNT(referrer) DESC; This should give you a two column set of records with the referrer in the first column and the number of referrals in the second. Mark
Should it not be something like COUNT(DISTINCT Referrer) ? I presume you cant do a mysql_fetch_row on a COUNT? What I want is the Referrer name and then the number of times it has been used displayed beside it. Might have to do some sort of multiple query.
The SQL I posted will give you what you want in two columns. Count(DISTINCT Referrer) will simply give you a count of the number of different referrers. Do you want the results in one column?
I think I need the results in 2 columns. 1 displaying the referrer name and another displaying the count of each distinct referrer name. I believe it's my fetching of the values that is perhaps causing the problems. Here is my exact code
TO me the fetching looks OK, try doing some debug code using: <?php $resulta = mysql_query("SELECT DISTINCT(refferrers) as referrer,COUNT(refferrer) as referrerCount from refferrers GROUP BY referrer ORDER BY COUNT(refferrer) DESC;"); if (!$resulta) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($resulta); echo $row[0]; // the referrer name echo $row[1]; // the referrer count (note the array offset) ?> Code (markup):
Well for one my table isnt called Refferrers, it's called Refferrer. Let me try that and see if it makes any difference. Ok it's displaying a list of results but the Count for each referrer is 1. www.weirfire.co.uk/Statistics.php grrr
Yeah that was my fault in the first place though. Sloppy stuff from Weirfire. I really should stop hacking code together. Any ideas how I can get the right count for each referral?
I think I want to do something like SELECT DISTINCT Referrer.R1, COUNT(R1) but I'm not sure. It's a lot easier plugging away at SQL with Oracle.
I suggest use only GROUP BY statement and omit DISTINCT (You can only use one column when using DISTINCT statement) SELEC Referrer, Count(*) as Count from Refferrer GROUP BY Refferrer
Thanks melfan and mnemtsas for your help. Got it working perfectly. I used melfans query without any distincts. I take it that the GROUP BY Referrer takes a unique referrer?