EzineArticles - Expert Authors Sharing Their Best Original Articles



  Submit Articles
  Members Login
  Benefits
  Expert Authors
  Read Endorsements
  Editorial Guidelines
  Author TOS

  Terms of Service
  Ezines / Email Alerts
  Manage Subscriptions
  EzineArticles RSS

  Blog
  Forums
  About Us
  What's New
  Contact Us
  Article Writing Shop
  Advertising
  Affiliates
  Privacy Policy
  Site Map


Advanced Search


Would you like to be notified when a new article is added to the Web-Development category?

Email Address:


Your Name:


Prefer RSS?
Subscribe to the
Web-Development
RSS Feed:

Create a Simple Hit Counter Using PHP and MySQL
Print This Article Ezine Publisher Send To Friends Add To Favorites Post A Comment Suggest Topic Report Author

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.

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

Note that for display considerations, in the following listings, opening and closing angle brackets for tag names ('<..>') are replaced by opening and closing square brackets ('[..]').

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.

[html][head][title]Create MySQL Database[/title][/head]
[body]
[?php

//This script creates a database on the MySQL server.
//The name of the database is counter.

//Connect to MySQL server
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

//If the connection cannot be made, display an error message
if (! $link)
die("Cannot connect to MySQL");

//Create a database called counter
mysql_create_db("counter")or die("Error: ".mysql_error());

//Close the connection to the MySQL server
mysql_close($link);
?]
[/body]
[/html]

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.

[html][head][title]Create Table in Database[/title][/head]
[body]
[?php

//This script creates a table (countertable) in the database (counter).

//Assign the name of the database (counter) to the variable $db.
$db="counter";

//Connect to MySQL server.
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

//If the connection cannot be made, display an error message.
if (! $link)
die("Cannot connect to MySQL");

//Select the database. If the database cannot be selected, display an error message.
mysql_select_db($db , $link)
or die("Select DB Error: ".mysql_error());

//Create a table called countertable in the database.
//The table contains one field: count, which should allow up to 99,999,999 hits
mysql_query("CREATE TABLE countertable( count INT(8))")or die("Create table Error: ".mysql_error());

//Close connection to MySQL server.
mysql_close($link);

?]
[/body]
[/html]

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.

[html][head][title]Reset Counter[/title][/head]
[body]

[?php

//Point your browser at this page to set/reset the counter to zero.

$db="counter";

$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

if (! $link) die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());

// Set the counter to zero
mysql_query("INSERT INTO countertable (count) VALUES ('0')");

//close link to MySQL server
mysql_close($link);
?]

[/body]
[/html]

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.

[html][head][title]Increment Counter[/title][/head]
[body]

[comment]
Include everything below this comment (down to the closing body tag) in the page
on which you want to put the counter.
[/comment]

[?php

//Set database to counter
$db="counter";

//connect to server and database
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

if (! $link) die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());

//Increment counter
mysql_query("UPDATE countertable SET count=count+1");

//extract count from database table
$counter = mysql_query("SELECT * FROM countertable");

//Display counter. If you want to change the appearance of the counter, edit
//the following table and font settings.
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>
print "[/tr]";
print "[/table]";
}

//close link to MySQL server
mysql_close($link);
?]

[/body]
[/html]

That's it!

About the Author: John Dixon is a web developer working through his own company John Dixon Technology Limited. The company also develops and supplies a free accounting - bookkeeping software tool called Earnings Tracker. The company's web site contains various articles, tutorials, news feeds, and a finance and business blog.

Article Source: http://EzineArticles.com/?expert=John_Dixon

John Dixon - EzineArticles Expert Author

Other Recent EzineArticles from the Internet-and-Businesses-Online:Web-Development Category:

Most Viewed EzineArticles in the Internet-and-Businesses-Online:Web-Development Category (90 Days)

  1. How to Create a Web Page Shadow Using Photoshop Slice Tool and HTML Coding
  2. Find Out the Best Free Website Creator
  3. Make Your Own Websites - What You Should Know
  4. Choosing Java Vs .Net For Web Development
  5. How to Put Hyperlink in an Image to Create a Clickable Picture!
  6. Under Construction Website - Create an Under Construction Web Page to Protect Yourself !
  7. How Much Does a Website Cost - An Inside Look From a Web Designer
  8. Creating Web Pages With PHP, CMS, and Joomla
  9. How to Create an HTML Image Background For Your Webpage
  10. Start My Own Website - Make Millions While I Sleep
  11. Build My Own Website in 2 Easy Steps
  12. How to Write Specifications For a Website
  13. Add a Flash Event Calendar to Your Website
  14. The Best Free Website Builder Service on the Internet
  15. Understanding the Basics of Owning Your Own Website

Most Published EzineArticles in the Internet-and-Businesses-Online:Web-Development Category

  1. Make a Website Free - 5 Benefits of Building a Website For Your Online Business
  2. 4 Reasons Why Its Important to Have a Website For Your Business - Or You Will Not Make Any Money
  3. Understanding the Basics of Owning Your Own Website
  4. How to Build a Website - Know More About It
  5. Make Your Own Website the Quick and Easy Way - Without Sacrificing Quality Or Options
  6. Make Money Online - Landing Page Creation
  7. How to Design a Social Networking Website
  8. Marketing Your Website Online
  9. Web Development Estimation
  10. Top 6 Website Basics You Must Know
  11. Under Construction Website - Without an Under Construction Web Page You Are in DANGER!
  12. Photography Website Music and Why it Hurts Your Business
  13. Creating Your Website - What You Need to Do to Make it LIVE!
  14. How to Build a Small Business Website For Less Than $100
  15. Photography Website How to For the Professional Photographer

 

This article has been viewed 5,208 time(s).
Article Submitted On: December 08, 2006



© EzineArticles.com - All Rights Reserved Worldwide.