Page 1 of 1

Trying to Create Record-Specific Session-Based Hit Counter

Posted: Sat Jun 23, 2007 2:35 pm
by slade27
I am trying to create a kind of session-based hit counter that will work with an existing database table of mine. The current database table already contains a number of fields, one of which is the "Articles" field, in which are stored (appropriately enough) various articles that folks can read (by accessing the database via clicking on the appropriate link on my site).

I would like now to add a numeric field to my table and call it "Count" and to have it increment by 1 each time a new site visitor accesses the database record with which it is associated.

In other words, if five different visitors click on the following link on my site....

Code: Select all

<a href="http://www.mysite.com/php/id.php?ID=27">Article Number 27</a>
(id.php being a short script that displays the Article in question and associated info)...

I would like the database record that includes ID 27 to have a Count field that now reads 5 as a result of those visits.

It seemed relatively easy to me to do this at first (and I think I even had it working for a moment, but I sort of "lost my place") but I've been working on it all morning and can't get it right.

Below you will find my latest TEST.PHP file, where, as you see, I've manually assigned the ID field as '1' in the hopes of getting the following script to augment the "Count" field for my first record by '1' every time I run it (or rather every time I run the script after first clearing my session data from my browser, of course, so that I'm a new visitor from the script's point of view).

The following script will successfully echo the "Title" field, but nothing appears for the "Count" field (though I have, of course, already added the Count field to my table -- as a VARCHAR type).

(I found the session-related part of the following script online under "simple hit counter.")

Code: Select all

<?php session_start();

include 'connect.php';

$ID = '1';

if (!session_is_registered("counted")){ mysql_query("UPDATE Articles SET count=(count + 1) WHERE ID LIKE '$ID'"); session_register("counted"); }

$sql="SELECT * FROM Articles WHERE ID LIKE '$ID'" ;
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {

echo $row['Title'];
echo $row['count'];
 }

?>
When I run the above script, the Article Title appears correctly; however, nothing at all appears for "Count" -- not even a zero.

My goal, once again, is to have each record in my database contain a "Count" field whose value will indicate the number of visitors who have accessed the record with which it is associated (i.e., who have accessed the record by having clicked on the sort of sample html link that I showed above).

I hope I've made the situation at least somewhat clear to you guys. Please let me know if you need any clarifications. And thanks in advance for any and all help!

Brian

Posted: Sat Jun 23, 2007 3:12 pm
by superdezign
Firstly, don't use session_is_registered() and session_register(). $_SESSION works just fine. The other functions are outdated.

Secondly, in your database table, is the `count` category of type INT and defaulted to 0?

And are you sure it isn't "Count" instead of "count?"

Posted: Sat Jun 23, 2007 3:31 pm
by slade27
Currently the data type for count is var char 255. Should I make it Int?

Regarding "count," I capitalized it in my post at times, but I think it's always been lower case whenever I've used it. I'll double check, though.

Posted: Sat Jun 23, 2007 4:14 pm
by superdezign
You cant ad an integer to a string, and varchar is a string. Also, varchar defaults to NULL, and you can't add anything to a NULL value. Change the data type to INT.

Code: Select all

ALTER TABLE `Articles` MODIFY COLUMN `count` INT DEFAULT 0;

Posted: Sat Jun 23, 2007 4:24 pm
by maliskoleather
superdezign wrote:You cant ad an integer to a string, and varchar is a string. Also, varchar defaults to NULL, and you can't add anything to a NULL value. Change the data type to INT.
you could also use intval();... but probably more work than its worth

Posted: Sat Jun 23, 2007 4:34 pm
by superdezign
maliskoleather wrote:you could also use intval();... but probably more work than its worth
Considering the whole process is performed through the database, PHP functions can't alter it. He'd have to retrieve the data, then typecast it and increment it, and then query the database to update it. That's one more query and one more operation than needed.

Posted: Sat Jun 23, 2007 4:39 pm
by maliskoleather
superdezign wrote:Considering the whole process is performed through the database, PHP functions can't alter it. He'd have to retrieve the data, then typecast it and increment it, and then query the database to update it. That's one more query and one more operation than needed.
exactly. i was just throwing it out as an option, though i should have specified a little better that its not a good idea.