Create a simple hit counter using PHP and MySQL

In this article I describe how to use PHP and MySQL to produce a simple counter that can be placed on a web page. PHP and MySQL work very well together, and this article shows, hopefully, how easy they are to use to produce a useful little utility.

In order for the counter to work, the web server you upload the files to needs to support PHP and MySQL. Most good hosting solutions do.

You can download the various scripts used to produce the counter from the following web address: http://www.computernostalgia.net/counter/counter.zip. The scripts are also listed below.

The counter needs a database called ‘counter’, a table in that database called ‘countertable’, and a field in the table called ‘count’. If you want to use a different database, table, or field name, make sure you change the appropriate references to these names in the scripts.

Files

The zip file (counter.zip) contains the following files:

  • create_database.php
  • create_table.php
  • reset_counter.php
  • counter.php

create_database.php

This script creates a MySQL database called ‘counter’. Upload this script to your web server and run it first to create the database.


<?php
$link = mysql_connect("localhost", "username", "password") or die("Cannot connect to MySQL");
mysql_create_db("counter")or die("Error: ".mysql_error());
mysql_close($link);
?>

create_table.php

This script creates a table (countertable) in the counter database. The table has one field, called ‘count’, which can store an eight digit number. This allows a counter value up to 99,999,999. Upload this and run it once the database has been created.


<?php
$db="counter";
$link = mysql_connect("localhost", "username", "password") or die die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Select DB Error: ".mysql_error());
mysql_query("CREATE TABLE countertable( count INT(8))")or die("Create table Error: ".mysql_error());
mysql_close($link);
?>

reset_counter.php

This script sets/resets the counter to zero. Upload this and run it to initialise the counter to zero. You can run it at any time to reset the counter to zero.


<?php
$db="counter";
$link = mysql_connect("localhost", "username", "password") or die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());
mysql_query("INSERT INTO countertable (count) VALUES ('0')");
mysql_close($link);
?>

counter.php

This is the actual counter. The code in this file should be pasted into the web page that will contain the counter (or it can be run on its own). This web page, which will typically be part of a web site, must have a .php file extension, otherwise the PHP code will be ignored by the web server.


<?php
$db="counter";
$link = mysql_connect("localhost", "username", "password") or die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());
mysql_query("UPDATE countertable SET count=count+1");
$counter = mysql_query("SELECT * FROM countertable");
print "<table border=1 cellpadding=3 cellspacing=0 width=80>";
while ($get_count = mysql_fetch_row($counter)){
print "<tr>";
foreach ($get_count as $field)
print "<td align=right><font face=arial size=2>$field</font></td>";
print "</tr>";
print "</table>";
}
//close link to MySQL server
mysql_close($link);
?>

That’s it!

About the Author: John Dixon is a web developer and technical author. He enjoys developing database-driven websites using PHP, Perl, and MySQL. John is also interested in anything to do with computer history. Visit http://www.computernostalgia.net/listDocumentsOutline.htm to see the current list of computer history articles on his site. To find out more about John, visit http://www.dixondevelopment.co.uk.



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Guest User

This author has not yet written a description. Please give them some time to get acquainted with the site and surely they will write their masterpiece.