PDA

View Full Version : SQL counting


PeterK2003
04-21-06, 21:45
ok i have a mySQL database which i strored visitors to my web page browser type in.(that sounds wierd to me so i hope it makes sense) What i want to do is count up how many of of each browser there is in the database. I know how to do some thing like this: select count(*) from database where BROWSER = "firefox 1.5". But i would like to make it automatic so when firefox 2.0 comes out it will pick it up by itself aka i don't want to have to hard code each browser in. Is there anyway to do this without iterating thought the whole database?

Publius
04-22-06, 00:32
If I understand your question correctly, you just need to generalize your queries.

Instead of:
select count(*) from database where BROWSER = "firefox 1.5
Use:
select count(*) from database where BROWSER like 'firefox%';
As long as your db tracks the browser by the name first this should work fine. In fact, you can use the wild-card (%) on both sides of defined parameters, so as long as there is some part of a string that is specific to each browser you should be able to track the browser independent of the version that a visitor is using.

Hope that helps.

PeterK2003
04-22-06, 00:38
not quite--i want say firefox 1.5 and firefox 2.0 to show up as different browsers. What i want is something so that everytime a new browser comes out i don't have to hard code it into my page. So in english i want to say "hey database i want to know all the different browser that were used to view my page and i want to know how many times each was used." Does that make sense? I know exacltly what i want but it is a little hard to convey. Thanks for the help!

covert215
04-22-06, 12:29
i don't know the language, but i'll help clarify

he wants to program it so that he does not have to set categories. when someone using firefox 1.5 visits the site, a category called firefox 1.5 will be started. if someone using xyz comes the site, a category for xyz will be started

talk2bks
04-22-06, 22:57
If I understand correctly, you will need 1 table with 2 rows (1: browser type, 2: number of hits).

1. in PHP set a variable that contains the name of the user's current browser.
2. connect to the database and count how many times that name appears.
3. if ( that browser != 0)
{add one to the "row count" where browser = "current browser"};
if (that browser = 0)
{add a new row with "that browser"}

4. to Display the information
select all the rows
number the affected rows
write loop to display all the affected rows.
echo out the names of all the browsers with the number of times it has been used.

hope this helps