Bizty Posted August 14, 2009 Share Posted August 14, 2009 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 More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 well $insert_q = mysql_query("SELECT * FROM stats"); doesn't check anything. it will always return more than 1 row (unless the table is empty) try adding a where clause to that query Link to comment https://forums.phpfreaks.com/topic/170308-simple-statistic/#findComment-898409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.