individual counter help

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
knmwt15000
Forum Newbie
Posts: 8
Joined: Thu Apr 25, 2002 9:51 am

individual counter help

Post by knmwt15000 »

I have written a stat counter for my site of the form

<?php

$db = mysql_connect("localhost", "root") or die ("unable to connect to server.<br>");

mysql_select_db("lithics_site",$db) or die ("Unable to connect to database.<br>");

$result = mysql_query("SELECT count(*) FROM counter",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<P class=normal>Site stats: %s Individual visits.</P>
",

$myrow["count(*)"]);
}
?>

does anyone know how I can have the server skip adding to the database if the ip address has already being listed. so the counter will show the number of individual computers that have accessed the site.

I have a feeling this can be done with if else statements but dont know how to impliment it.
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

a couple of things you could do to get the number of indiviual ip addresses:
easiest would be to just change the query to:

Code: Select all

SELECT count(DISTINCT ip) FROM counter
this wouldn't stop the adding of rows with the same ip though, but shouldn't require any change to your table, and means you can still get other stats like total page hits.

You could make the ip field unique, and then any attempt to insert a hit with the same ip would fail.

Or you could use a select query to see if a record with the ip address already exists and only add the new one if it doesnt.

Hope this helps, Sam
User avatar
knmwt15000
Forum Newbie
Posts: 8
Joined: Thu Apr 25, 2002 9:51 am

Post by knmwt15000 »

thanks mate.

I actually made the field unique yesterday but I'm going to change it back and use your first suggestion.

cheers
aybra
Forum Commoner
Posts: 56
Joined: Sun Nov 24, 2002 12:52 am

Post by aybra »

I know this is an insanly old post origanlly... but I thought I'd add my two since worth...

You could run a DELETE FROM querry as well just make sure to tage LIMIT 1 to the end of it.. or you'll delete both ip's.

Also make sure to run it after the entry, and before the stats output... it may be just a couple of extra steps, but its something I did with an update page for incoming payments to keep me from accidently inserting nasty blank fields all over the place.
Post Reply