Join Login

Video Tutorials

Create a Simple View Counter

By: RiverCityGraphix      Views: 13875      Share: Bookmark and Share

In this tutorial, you will learn how to create a simple view counter with PHP and MySQL.

A view counter can be a very simple and elegant way to display how many people have viewed a specific page. PHP and MySQL make this a very easy task. First you will need to create a table in your database that that will hold the information for your view counter. If you want to add views to a database you already have you will simply need to add an additional row as you should already have the id and any other necessary data in the database.

Once you have the database in order you will need to enter some data either manually such as I did in the tutorial, or dynamically by another means. Once you have data you can connect to your database with the file we created and then query out the data and display it.

This view counter is not super accurate as I discussed in the video. The counter will increase by 1 every time the page is refreshed. This means that the same person could add multiple views. In most cases this is perfectly fine but I thought I would mention it.

If you want to see this in action, the view counter in the box at the top of the page uses this functionality. Just refresh the page and it will increase.


include_once "mysql_connect.php";

mysql_query("UPDATE viewcounter SET `views` = `views`+1 WHERE id='1'");

$sql = mysql_query("SELECT * FROM viewcounter WHERE id='1'");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$pagename = $row["pagename"];
$views = $row["views"];
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


There are <?php print $views; ?> views on the <?php print $pagename; ?> page!


Free Source Files Zip

Yahoo! Small Business