Jump to content

Simple statistic


Bizty

Recommended Posts

Ok, now I'm quite puzzled by this one (I think I'm having mind tricks now a days).

 

I have a simple stat function that only checks if the IP is in the DB if it is, then add a visit and if not, add it as a new entry, code:

 

function GatherStats($ip)
{
$insert_q = mysql_query("SELECT * FROM stats");
if(mysql_num_rows($insert_q) == 0)
{
	mysql_query("INSERT INTO stats (ip, visits) VALUES ('$ip','1')");
}
else
{
	while($irow = mysql_fetch_array($insert_q))
	{
		$new_visits = 0;
		if($ip == $irow['ip'])
		{
			$new_visits = $irow['visits'] + 1;
			mysql_query("UPDATE stats SET visits='$new_visits' WHERE ip='$ip'");
		}
		else
		{
			mysql_query("INSERT INTO stats (ip, visits) VALUES ('$ip', '1')");				
		}
	}
}
}

 

And on the index:

 

$ip = $_SERVER['REMOTE_ADDR'];
GatherStats($ip);

 

so that in my (maybe blind) eyes that looks okay, but what there's really happening is that:

 

1: It gets the IP

2: It checks in the DB if there's a duplicate

3: If there is, it adds mysql_num_rows(); - 1(the duplicate) new entries in the DB, _instead_ of just adding a new visit.

 

so it seems that if it fills the requirements in the if($ip == $irow['ip']) then it goes to the }else{

 

I am obviously missing something but I am oblivous to what (too much coding lately I reckon)

Link to comment
https://forums.phpfreaks.com/topic/170308-simple-statistic/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.