Firefox crashed the first time I wrote this so I had to re-do it. :'(
Creating a page view counter in PHP and MySQL
This short guide will allow you to add a counter to your pages. In order for this counter to work, you will need:
- A professionally hosted website
- MySQL
If you lack either of those things (meaning you have a free hosted website, or something besides MySQL) then do not continue on with this guide, as it will be no help to you.
Beyond that, you will be able to create your counter. The rest of this guide is useful for you only if you have:
- CPanel
- MySQL Administration in CPanel
- PHPMyAdmin
If you lack any of those things, contact me so I can write you a script which will take the place of this guide. If you don't know if you have these things, follow steps 1 and 4 to find out. If you cannot find any of the things in steps 1 and 4, you do not meet the requirements.
If you've met all the requirements so far, follow these steps to create your counter.
PLEASE NOTE: You may click any picture from this point on to see a larger view.
Step 1: Locating your MySQL administration
1. Open up CPanel.
2. Locate the MySQL administration icon, which should look something like this:
3. Click that icon.
Step 2: Creating your database
1. Locate this box:
2. Type into the box "hits" and hit the "Create Database" button.
3. When you create your database, a prefix may be added. To makes sure you know the real name of your database, look for it in the list here:
4. Write down the name of your database.
Step 3: Creating a user and adding it to the database
1. Locate this box:
2. Type in a username and password to use.
3. Hit the "Create User" button.
4. Locate the "Current Users" right above the boxes you typed the username and password into. Check the username, because a prefix may have been added.
4. Write the username and password down.
5. Locate the "Add users to your databases" section and configure it like this:
6. Hit the "Add User to Database" button.
Step 4: Locating PHPMyAdmin
1. Find something that looks like this:
2. Click that link.
Step 5: Adding a Table
1. Find your database on the left side and click it. It should look something like this:
2. Find this and type in "hits" for the name and "2" for number of fields:
3. Configure it like this:
Bookmark 1
4. Now go click this:
5. Set it up like this and click the "Go" button in the right below the two fields you filled out, and make sure you have the "ignore" box checked like shown in the picture:
END BOOKMARK
Step 6: Adding the Counter
1. Make your page a .php file instead of a .html or .htm if you havn't done so already. (For some people this may require you to either re-upload the file as a .php or just to rename the file.)
2. Add the following code anywhere inside the <body> tag where you want your counter number to appear replacing all instances of USERNAME, PASSWORD, and DATABASE. If you configured this counter to be for a different page than the home page, replace all instances of "home" with your page name, as well. :
Code:
<?php
$connection = mysql_connect("localhost","USERNAME","PASSWORD");
mysql_select_db("DATABASE",$connection);
$result = mysql_query("SELECT * FROM hits WHERE page='home'") or
die(mysql_error());
$row = mysql_fetch_array($result);
$temp = $row["number"]+1;
mysql_query("UPDATE `DATABASE`.`hits` SET `number` = '$temp' WHERE CONVERT( `hits`.`page` USING utf8 ) = 'home' LIMIT 1");
echo $temp;
?>
NOTE: If you'd like to add this to multiple pages in which each page has its own counter, repeat Bookmark 1 and Step 6 with a different page name.
THAT'S IT, now you've got a working page view counter!
I hope this guide helped, it took me a quite a while to complete.
This guide © 2008 Lumentec_Dark. All rights reserved.
------------------------------
-------------------------
--------------------
---------------
----------
-----
-

