Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Hit Counter in php

New Here ,
Dec 18, 2008 Dec 18, 2008
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!
TOPICS
Server side applications
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 19, 2008 Dec 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 Powe...
Translate
LEGEND ,
Dec 19, 2008 Dec 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2008 Dec 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 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?
>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2008 Dec 19, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 19, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2008 Dec 19, 2008
LATEST
I GOT IT THANKS GUYS!!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines