PDA

View Full Version : Help with hit counter code - can't display number of hits


manicman
08-07-2006, 07:52 PM
This is the code that I am using for my hit counter, could someone please tell me how I would display the number of hits on my webpage. As when I try I always get an 'undefined variable' error.


$gethitssql = "select * from `hits`";
$gethitsres = mysql_query($gethitssql) or die("SQL: " . $gethitssql . "<br />" . mysql_error());
if ($h = mysql_fetch_row($gethitsres)) {
$oldhits = $h[0];
$newhits = $oldhits + 1;
$addhitsql = "update `hits` set `hits`='" . $newhits . "'";
$addhitres = mysql_query($addhitsql) or die("SQL: " . $addhitsql . "<br />" . mysql_error());
}


Please tell me if there are any other errors in my code.
Thanks :)

Fumigator
08-07-2006, 08:18 PM
Your table is named hits and the field in the table is called hits? Kinda confusing... I'd give them better names were it me. That might even fix your problem (not sure about that).

To display the hits you would add an echo statement and echo $newhits.

What line are you getting "undefined variable" on? Can you copy/paste the entire error text?

manicman
08-08-2006, 08:25 AM
Yep, I thought that it would be 'echo $newhits' and tried it before posting the topic. That is what gave me the error.

Here is a copy of the entire error code:
PHP Notice: Undefined variable: newhits in D:\Documents and Settings\Administrator.THAKES_SERVER\Desktop\Intranet\Development\Website\views.php on line 26

and here is a copy of my entire views.php file:

<html>
<head>
<title>Test Hit Counter</title>
</head>
<body>
Visitor number:

<?php
//CONNECTION
$link = mysql_connect( "localhost", "root", "password" );
if ( ! $link ) {
die( "Couldn't connect to MySQL" );
}
$database = "website";
mysql_select_db( $database ) or die ( "Couldn't open $database" );

//SCRIPT
$gethitssql = "select * from `hits`";
$gethitsres = mysql_query($gethitssql) or die("SQL: " . $gethitssql . "<br />" . mysql_error());
if ($h = mysql_fetch_row($gethitsres)) {
$oldhits = $h[0];
$newhits = $oldhits + 1;
$addhitsql = "update `hits` set `hits`='" . $newhits . "'";
$addhitres = mysql_query($addhitsql) or die("SQL: " . $addhitsql . "<br />" . mysql_error());
}
echo "$newhits";

?>

</body>
</html>

NancyJ
08-08-2006, 12:36 PM
if $newhits is undefined on line $26 then your if() is failing...
You could also streamline your code quite a bit.


$sql = "UPDATE hits SET hits = hits+1";
mysql_query($sql) or die("SQL: $sql.<br />".mysql_error());
$sql = "SELECT * FROM hits";
$result = mysql_query($sql);
if($h = mysql_fetch_row($result))
{
echo $h[0];
}
else
{
mysql_query("INSERT INTO hits (hits) VALUES (1)") or die("Cannot insert into hits because ".mysql_error());
echo 1;
}

what it basically does is increments your hit counter, then fetches the new number from the database, if a row is found, it echos out the new number, if the query ran sucessfully but $h cannot be set to mysql_fetch_row then it assumes that there were no rows in your table and inserts one with a vistor value of 1.

Ofcourse as fumigator mentioned, you have the same table and fieldname, I dont know the implications of that, it may be the root of your problem.

manicman
08-08-2006, 04:22 PM
Thanks very much for the code. I have got it working now with both the table and field named hits so for future reference that should not be a problem.

Thanks very much again for the help. :thumbsup: