on January 1, 2010
Create a simple view counter using PHP and MySQL.
This tutorial relies on server side programming languages. To begin, be sure you have XAMPP or a similar localhost software running for testing purposes.
Get Xampp
In this tutorial, you will learn how to make a view counter using a few lines of PHP and MySQL. We will also be playing around in phpMyAdmin to create a basic table. This is not a real use case for this technique, you will most likely be adding this to a table on the database that contains profile information or a similar table.
Step 1
We begin the tutorial with a basic HTML page, with a PHP brackets in topmost part of the document. This is where we will place the database connection information. Your localhost may be set up differently than this, please familiarize yourself with your username and password of your server.

Step 2
In good practice, declare variables with data that will be used more than once. This includes “host” which in our case is localhost. This will differ if you are using a live server, or are using this on an application in a website. “username” is the username for the server. Default username for localhost is ‘root’. “password” is naturally the password to the server, again default for local host is ‘none (represented with empty quotes)’. “dbname” should contain the name of the database you are trying to reach. In this case we have created a database named ‘views’. We will go further into this in a moment. ”tblname” represents the table inside of the database that contains the information we desire, in our case we named it ‘counter’.

Step 3
Its time to open your phpMyAmdin. This is where we can access, and manipulate database data. Depending on software and platform, your URL may be different. (http://localhost/phpmyadmin/).
Find the section that allows you to create a new database, shown below. We will be making a new database named “views” as we have coded into the variables in step 2.

Step 4
Now, we will need to add a table and a row of data. Start off by making a table and naming it “counter” as we have coded in the variables on step 2. Then we will need 1 field of data on this table. (As mentioned before, this is not a real life use case. In actual use, you would add a field to an existing table.)

Step 5
After creating a new table, you will be presented with the field form. The way you make this is really up to you, for this tutorial we will just do a couple of things. The filed is named “hits” and the length is set to “5” the highest number this counter can hit will be ’99999′.

Step 6
There are several ways you can do this through PHP, for this tutorial we will do it through phpMyAmdin. Insert a new row, and set the initial value to ’0′. This will ensure the counter will start at 0 and the first view will be counted as 1.

Step 7
We are now done with phpMyAdmin. Lets get back to the code. We have our database set up, and we have all our variables with the names set up. Now we need top connect to the server and database.
To do this, we create a new variable named “con” for ‘connection’. The name of this variable is not important, however you should have a clear idea of what variable is when looking at the name.
this function we will use to contact the server. Inside the parenthesis is where the variables containing the server details go.
After the parenthesis, to make sure nothing executes unless the server is connected add the function ‘die()’ This will stop the script from running any further.
We also need to connect to the correct database, in our case the name is ‘views’. To do this is similar to connecting to the server.
Inside the parenthesis is where the variable containing the name of the database goes “$db_name”. Just like above, use the ‘die()’ function to stop script execution if there is an error connecting.
Full code for connecting to server and database
$host="localhost";
$username="root";
$password="";
$dbname="views";
$tblname="counter";
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
?>
Step 8
Now that we are all connected, we are ready to start the fun part. Obtaining data from the database, displaying it, and modifying it. To do this we first create a variable named ‘query’.
Inside the variable we access the database using SQL – mysql_query()
“SELECT hits FROM $tbl_name”
This will select the row ‘hits’ from the table ‘counter’.
Then we need to count how many rows are in the ‘hits’ block. Create a new variable named ‘numrows’.
Step 9
Next we add an if-while loop.This will make sure the variable numrows has data in it, and to store that data in variables as needed.
while ( $row = mysql_fetch_assoc($query)) {
$views = $row['hits'];
}
}
This grabs the data inside the query, and stores them all in an array variable named ‘row’. We want to be a little more specific, so we add a variable named views and have it be the same as the hits-row.
Step 10
At this point the information is in the script, you just need to display it. To do this, we are going to add a print() function. Adding the variable to the print data so it is on the screen.
Step 11
Everything works, and now we need to count up and add the new number to the database. This part is quite simple. Take the variable ‘views’ and add 1 to it on every page load.
We then need to put the new number in the database. To do this requires a little bit of SQL.
Use the same ‘query’ variable and structure as we have above.
The SQL for this action is a little different than selecting. To place a new value in the database requires the following query “UPDATE $tblname SET hits=’$views’”
This will update the ‘counter’ table and set the row ‘hits’ with the value of the variable ‘views’.
Full code in body section
$query = mysql_query("SELECT hits FROM $tblname");
$numrows = mysql_num_rows($query);
if($numrows !=0) {
while ( $row = mysql_fetch_assoc($query)) {
$views = $row['hits'];
}
}
print("There has been $views on this page");
$views = $views + 1;
$query = mysql_query("UPDATE $tblname SET hits='$views'");
?>
Final
Your view counter is done, Watch as it counts up. Questions and comments will be answered below. Thanks for reading the tutorial.
You can leave a response, or trackback from your own site.
4 Responses to “Creating a basic View Counter using PHP and MySQL”
-
Creating a basic View Counter using PHP and MySQL | Coder Online Says:
[...] Read this article: Creating a basic View Counter using PHP and MySQL [...]
-
Inkjet Printer Says:
Just want to say your article is striking. The clarity in your post is simply striking and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with forthcoming post. Thanks a million and please keep up the ac complished work. Excuse my poor English. English is not my mother tongue.
-
slotsonline Says:
I admit, I from not been on this webpage in a lengthy time… in what way it was another buoyancy to conceive of It is such an distinguished thesis and ignored close so multitudinous, imperturbable professionals. I because of you to eschew making people more aware of possible issues.
-
Richard Hammed Says:
howdy there, i just came across your website on bing, and i must comment that you write exceptionally good via your site. i am very struck by the mode that you write, and the subject is outstanding. anyhow, i would also love to know whether you would like to exchange links with my site? i will be to the great extent than happy to reciprocate and insert your link on in the blogroll. looking for your reply, i would like to convey my appreciation and have a great day!




