Tuesday, 4 July 2017

Create a Simple Hit Counter Using PHP and MySQL

In this text I describe how to use php and MySQL to produce a easy counter that may be located on a web web page. Php and MySQL work very well together, and this text shows, optimistically, how easy they may be to use to provide a useful little application.

So as for the counter to paintings, the web server you upload the documents to desires to support Hypertext Preprocessor and MySQL. Most suitable website hosting solutions do.

The counter wishes a database referred to as 'counter', a table in that database called 'countertable', and a discipline within the table referred to as 'count'. In case you need to use a specific database, desk, or field call, make certain you change the appropriate references to those names inside the scripts.

Files

The zip record (counter.Zip) incorporates the following files:

create_database.Hypertext Preprocessor
create_table.Personal home page
reset_counter.Personal home page
counter.Php
be aware that for display issues, inside the following listings, beginning and ultimate angle brackets for tag names ('') are changed via establishing and ultimate square brackets ('[..]').
Create_database.Php

This script creates a MySQL database called 'counter'. Upload this script in 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) within the counter database. The desk has one discipline, called 'count', which can keep an eight digit variety. This lets in a counter value as much as 99,999,999. Add this and run it once the database has been created.

[html][head][title]Create desk 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 units/resets the counter to zero. Add this and run it to initialise the counter to zero. You could run it at any time to reset the counter to 0.

[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 real counter. The code in this file must be pasted into the internet page with a view to contain the counter (or it is able to be run on its personal). This internet web page, for you to commonly be a part of a web website, should have a .Php document extension, otherwise the php code could be omitted with the aid of the internet server.

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

[comment]
encompass the whole lot beneath this comment (down to the remaining frame tag) in the page
on that you need to position the counter.
[/comment]

[?Hypertext Preprocessor

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

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

//in case you want to deliver a username and password, then use the subsequent line
//of code instead of the only above, substituting the appropriate username and password.
//$hyperlink = mysql_connect("localhost", "username", "password");

if (! $hyperlink) die("can not connect to MySQL");
mysql_select_db($db , $hyperlink) or die("cannot open $db: ".Mysql_error());

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

//extract be counted from database table
$counter = mysql_query("select * FROM countertable");

//show counter. In case you need to alternate the appearance of the counter, edit
//the subsequent table and font settings.
Print "[table border=1 cellpadding=3 cellspacing=0 width=80]";
at the same time as ($get_count = mysql_fetch_row($counter))
print "[tr]";
foreach ($get_count as $area)
print "[td align=right][font>
print "[/tr]";
print "[/table]";


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

[/body]
[/html]

that is it!

No comments:

Post a Comment