PHP from database and query
Posted
by
Kyle R
on Stack Overflow
See other posts from Stack Overflow
or by Kyle R
Published on 2011-01-09T11:34:27Z
Indexed on
2011/01/09
11:53 UTC
Read the original article
Hit count: 129
I have a table:
id, affiliate
Each time somebody clicks a link, a new row is inserted,
ID being the ID of the page, and affiliate being the ID of the affiliate.
For example:
Page ID: 9 Affiliate ID: 1
Page ID: 9 Affiliate ID: 2
Page ID: 9 Affiliate ID: 3
I only have 3 affiliates.
I want to select this information, and group them by affiliate, for the ID.
I have tried this query:
SELECT COUNT(*) FROM
table
WHEREid
= '9' GROUP BYaffiliate
It works fine when I do it in php my admin, how do I get the info in PHP?
I have tried:
$q = mysql_query("SELECT COUNT(*) FROM
table
WHEREid
= '" . $id . "' GROUP BYaffiliate
");$r = mysql_fetch_array($q);
When trying to print the data onto the page, I am only getting one result.
Do I need to use a foreach/while loop to get all 3? How would I go about doing this?
Thank you!
© Stack Overflow or respective owner