View Full Version : Visit Counter

Jon W
03-02-2009, 09:38 PM

$host = "localhost";
$username = "root";
$pass = "";

$con = mysql_connect($host, $username, $pass);
if(!$con) {
die('Server Error: ' . mysql_error());

mysql_select_db("site", $con);


$sql = mysql_query("SELECT ip FROM visits WHERE ip='$ip'") or die("Server Error: " . mysql_error());

$check = mysql_num_rows($sql);

if($check['ip'] >= 1) {

$fetch = mysql_query("SELECT amount FROM visits WHERE ip='$ip'") or die('Server Error: ' . mysql_error());

$num = mysql_fetch_assoc($fetch);

$visited = $num['amount'] + 1;

mysql_query("UPDATE visits SET amount='$visited'") or die('Server Error: ' . mysql_error());

}else {

$new = mysql_query("INSERT INTO visits (`ip`,`amount`) VALUES('$ip','1')") or die("Server Error" . mysql_error());




Basically I'm trying to build a counter that will count how many times that person with that ip has came to one of my pages. My problem is that whenever I refresh the page its adding another column in the database and not taking the already existing number and updating that number thats in the database by one. So I wonder if you guys could help me out on this. I'm sure its something simple that I'm overlooking. Thanks for the help guys.

03-02-2009, 10:25 PM
$check = mysql_num_rows($sql);

if($check['ip'] >= 1) {

Your variable $check is not an array; it's an integer. Use it without the index reference.