Skip to main content
Participant
December 19, 2008
Answered

Hit Counter in php

  • December 19, 2008
  • 7 replies
  • 1037 views
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 has been closed for replies.
Correct answer Newsgroup_User
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
December 20, 2008
I GOT IT THANKS GUYS!!!
Inspiring
December 20, 2008
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
jh369057Author
Participant
December 20, 2008
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.
Newsgroup_UserCorrect answer
Inspiring
December 19, 2008
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/
Inspiring
December 19, 2008
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?
>
jh369057Author
Participant
December 19, 2008
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?
Inspiring
December 19, 2008
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/