Closed Thread Icon

Topic awaiting preservation: cont different Pages that link to <a href="https://ozoneasylum.com/backlink?for=26885" title="Pages that link to Topic awaiting preservation: cont different" rel="nofollow" >Topic awaiting preservation: cont different\

 
Author Thread
EDDII
Bipolar (III) Inmate

From:
Insane since: May 2004

posted posted 10-22-2005 21:58

oh what i have is a list of ip adresses and pages that they viseted in my a table but each page they visited is a seperate row so how can i count all the different ip adressess to determine unique visitors

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 10-22-2005 22:12

MySQL: SELECT COUNT(DISTINCT(`user_ip`)) FROM `my_table`

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-24-2005 06:45

If you're going to count like that, you should probably assign the result to something so it can be called up later on, so 'SELECT COUNT(DISTINCT('user_ip')) AS unique_ip FROM my_table' would be more appropriate I think


Justice 4 Pat Richard

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 10-24-2005 13:50

What is the advantage to doing it that way skaarjj?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-24-2005 17:51

It just names the result. I think if you don't do it that way you end up with something like mysql_result_array['count'] or something like that.



.:[ Never resist a perfect moment ]:.

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 10-24-2005 21:33

Right.

now whenever I call anything out of the DB, I assign it to a variable name in PHP - in this case something like $unique_ip.

is there any actual advantage to adding the "as" part in a case like this?

So far I've only used such things when I am combining or calculating values.

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 10-25-2005 00:04

My reason (in context) for not using 'AS':

code:
// MySQL connection open and database selected...
$query = "SELECT COUNT(DISTINCT(`user_ip`)) FROM `my_table`";
$result = mysql_result($query);
$ip_count = mysql_result($result,0);
// Whatever else here...



Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-25-2005 07:39

Which is perfectly valid. I usually, when i'm counting fields, always apply them to names with AS since when I'm doing it I end up doing multiple counts of interrelated fields (now we get into complex, multi-pathed SQL statements) so assigning them ot names helps to keep things clear. In a single count situation as this it's not necessary, it's just habit.


Justice 4 Pat Richard

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 10-25-2005 15:44

Thanks for clarification =)

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 10-25-2005 20:14

Another way to do this is to take each ip returned from " SELECT ip FROM site_info " and assign each item returned to an assosiative array with the name of the value returned.

code:
while ($row=mysql_fetch_array($result)){
		
             foreach ($row as $key => $value){
			
			$ip[$value]=$value;
			
			
		}
		
		
}



You are then left with an array of unique values only.

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 10-26-2005 01:05

True, but whyp ut your server through the extra processing load when you can just get the SQL to count it all up for you?


Justice 4 Pat Richard

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 10-26-2005 01:44

You wouldn't, if you were just counting. But you would if you wanted to list the unique IP's instea of just counting them.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-26-2005 01:52
quote:
But you would if you wanted to list the unique IP's instea of just counting them.

in that case why not simply doing :

code:
MySQL: SELECT DISTINCT(`user_ip`) FROM `my_table`



norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 10-26-2005 06:24
quote:

True, but whyp ut your server through the extra processing load when you can just get the SQL to count it all up for you?



Most of the time we are hitting a MySQL database (localhost)on the same box as the webserver so the same server handles the load either way. I'm not dead sure about this, but I'm willing to bet that the database is more efficient at this than php though...

I just wanted to provide an another way of doing this sort of thing. this way will work with data from xml, csv, and any other data source.

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 10-26-2005 15:34
quote:

poi said:

in that case why not simply doing :
code:

MySQL: SELECT DISTINCT(`user_ip`) FROM `my_table`



Mainly because I simply wasn't thinking when I posted =)

« BackwardsOnwards »

Show Forum Drop Down Menu