I am trying to incorporate a simple hit counter into my web page with the following code.....but it does not display the value of counter. It just displays "You are visitor " and not value for counter. The counter field in the table is getting updated successfully. There are only 2 fields in the table page and counter.

<?php
$db=mysql_connect("localhost", "username", "password");
mysql_select_db(homenetguy,$db);
mysql_query("UPDATE hits SET counter=(counter + 1) WHERE page='$PHP_SELF'",$db);
if (mysql_affected_rows($db) < 1) {
$result = mysql_query("INSERT INTO hits VALUES('$PHP_SELF',1)",$db);
}

$result = mysql_query("SELECT counter FROM hits",$db);
while ($row = mysql_fetch_row($result)) {
print ("You are visitor ");
print $row["counter"];
}
?>

    I think the problem is that you're using mysql_fetch_row, which requires a number to show the position of the fields within your query array. In your case the print statement should read:

    print $row[0];
    

    If you want to use the actual field name use mysql_fetch_array, which returns an associative array that can use both numbers and field names.

      wow that was it, thanks. That was driving me nuts. I guess I was looking at it too long.

      Thanks again.

        Write a Reply...