Skip to main content
Participant
Dec 19, 2008 2:25 AM UTC
Answered

Hit Counter in php

Ok I have found many forums on this topic but none that answer my question. I want to add a visitor count to a detailed record page that count the hits for a specific record. (eg. http://nameofmysite.com/detailed.php?recordID=300)

I dont need the results to be displayed on any page but I do want to record the number in my main database named 'Page' in the 'Hits' Field. A session Variable would be nice. I am ultimately trying to create a top ten section based on the number of hits. Please Help!
This topic is closed to new replies. Start a new post to keep the conversation going.
Newsgroup_UserCorrect answer
Inspiring
Dec 19, 2008 11:02 PM UTC
jh369057 wrote:
> Thanks for the quick response. This is what my code actually looks like right
> now... I am not getting any errors but Im not getting results

You're not connecting to the database.

> $dbh = mysql_connect($hostname, $username, $password)
> or die("Unable to connect to MySQL");

You need to insert a connection to the database here:

mysql_select_db('databaseName');

> $sql = 'UPDATE page SET Hits = (Hits + 1) WHERE Number = '.
> mysql_real_escape_string($rec);

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS4",
"PHP Solutions" & "PHP Object-Oriented Solutions"
http://foundationphp.com/

7 replies

jh369057Author
Participant
Dec 20, 2008 1:48 AM UTC
I GOT IT THANKS GUYS!!!
Translate
Inspiring
Dec 20, 2008 1:00 AM UTC
jh369057 wrote:
> Thanks David for the help. Im definitely connecting to the database but still
> update of the Hits field. Ive only been working with(or heard of) php for
> about a month and i am trying to understand and learn as I go. I can see what
> the code is doing here until I get to the end of this line...
> > $sql = 'UPDATE page SET Hits = (Hits + 1) WHERE recordID = '.
>
> What is it doing with the '. after it get the recordID url parameter.
>

The dot concatenates the parts of the expression.
Mick
Translate
jh369057Author
Participant
Dec 20, 2008 12:50 AM UTC
Thanks David for the help. Im definitely connecting to the database but still update of the Hits field. Ive only been working with(or heard of) php for about a month and i am trying to understand and learn as I go. I can see what the code is doing here until I get to the end of this line...
> $sql = 'UPDATE page SET Hits = (Hits + 1) WHERE recordID = '.

What is it doing with the '. after it get the recordID url parameter.
Translate
Newsgroup_UserCorrect answer
Inspiring
Dec 19, 2008 11:02 PM UTC
jh369057 wrote:
> Thanks for the quick response. This is what my code actually looks like right
> now... I am not getting any errors but Im not getting results

You're not connecting to the database.

> $dbh = mysql_connect($hostname, $username, $password)
> or die("Unable to connect to MySQL");

You need to insert a connection to the database here:

mysql_select_db('databaseName');

> $sql = 'UPDATE page SET Hits = (Hits + 1) WHERE Number = '.
> mysql_real_escape_string($rec);

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS4",
"PHP Solutions" & "PHP Object-Oriented Solutions"
http://foundationphp.com/
Translate
Inspiring
Dec 19, 2008 10:32 PM UTC
jh369057 wrote:
> Thanks for the quick response. This is what my code actually looks like right
> now... I am not getting any errors but Im not getting results
>
> <?php if (isset($_GET['Number'])) {
> $rec = $_GET['Number'];
> $username = "root";
> $password = "jhsjdfjfj";
> $hostname = "localhost";
> $dbh = mysql_connect($hostname, $username, $password)
> or die("Unable to connect to MySQL");
>
> $sql = 'UPDATE page SET Hits = (Hits + 1) WHERE Number = '.
> mysql_real_escape_string($rec);
> mysql_query($sql);
> }?>
>
> "Number" = field that has recordID stored
> "page" = name of database
> "Hits" = Where I want the hits stored

"page" should be the name of the table, not the database.
Mick


>
> I tried different iterations of this code with recordID but can not seems to
> get any results. Any thoughts?
>
Translate
jh369057Author
Participant
Dec 19, 2008 10:18 PM UTC
Thanks for the quick response. This is what my code actually looks like right now... I am not getting any errors but Im not getting results

<?php if (isset($_GET['Number'])) {
$rec = $_GET['Number'];
$username = "root";
$password = "jhsjdfjfj";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

$sql = 'UPDATE page SET Hits = (Hits + 1) WHERE Number = '.
mysql_real_escape_string($rec);
mysql_query($sql);
}?>

"Number" = field that has recordID stored
"page" = name of database
"Hits" = Where I want the hits stored

I tried different iterations of this code with recordID but can not seems to get any results. Any thoughts?
Translate
Inspiring
Dec 19, 2008 12:43 PM UTC
jh369057 wrote:
> Ok I have found many forums on this topic but none that answer my question. I
> want to add a visitor count to a detailed record page that count the hits for a
> specific record. (eg. http://nameofmysite.com/detailed.php?recordID=300)

When you create a new record, set the value of the Hits field to 0.

In detailed.php add the following code:

if (isset($_GET['recordID'])) {
$rec = $_GET['recordID'];
// add your database connection code here

$sql = 'UPDATE nameOfTable SET Hits = (Hits + 1) WHERE recordID = '.
mysql_real_escape_string($rec);
mysql_query($sql);
}

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS4",
"PHP Solutions" & "PHP Object-Oriented Solutions"
http://foundationphp.com/
Translate