|
|
|
|
| |
Creating a counter, whether it be graphical or textual, is much easier than it looks. This tutorial will lead you through the various steps, providing sample code along the way.
Our first step is to create a text-based counter. This tutorial will illustrate how to store the counter data in Mysql, but text files and other databases can be used as well.
Let's start by creating the MySql table that we'll need. |
|
CREATE TABLE counter (
id int(11) DEFAULT '0' NOT NULL auto_increment,
name varchar(250) NOT NULL,
count int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
);
|
|
|
The first column, id, is the primary key. This is not absolutely necessary, yet it's nice to have.
Secondly, we have name, which is the Script Name. This is needed because we plan to have multiple counters.
Lastly, we have count, which is where we keep track of how many hits each page has had.
Firstly, we need to connect to the database. |
|
<?
$hostname = 'localhost';
$username = 'some_username';
$password = 'some_password';
$dbName = 'some_database';
MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to
database");
@mysql_select_db("$dbName") or die("Unable to select database");
?>
|
|
|
|
Now lets ask the database to retrieve all rows where $name = 'some unique name'. Any $name can be used (ex 'homepage', '2nd page', etc) but for this example we are setting $name equal to $SCRIPT_NAME. Doing this will make every page have its own unique counter without having to change the code one bit. |
|
<?
$hostname = 'localhost';
$username = 'some_username';
$password = 'some_password';
$dbName = 'some_database';
MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to
database");
@mysql_select_db("$dbName") or die("Unable to select database");
$name = "$SCRIPT_NAME";
$result = MYSQL_QUERY("SELECT * FROM counter WHERE (name = '$name')") or die
("Bad query: ".mysql_error());
$row = mysql_fetch_array($result);
?>
|
|
|
I thought it'd be really neat if new counters, or rows in the database, could be added automatically. This can be done quite easily. All we need is an if() statement to check if the row already exists. If it doesn't, we simply add another row. |
|
<?
$hostname = 'localhost';
$username = 'some_username';
$password = 'some_password';
$dbName = 'some_database';
MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to
database");
@mysql_select_db("$dbName") or die("Unable to select database");
$name = "$SCRIPT_NAME";
$result = MYSQL_QUERY("SELECT * FROM counter WHERE (name = '$name')") or die
("Bad query: ".mysql_error());
$row = mysql_fetch_array($result);
if($row){
MYSQL_QUERY("UPDATE counter SET count = count+1 WHERE (name = '$name')") or die
("Bad query: ".mysql_error());
$count = $row['count'];
}else{
MYSQL_QUERY("INSERT INTO counter VALUES ('', '$name', '2')") or die("Bad
query: ".mysql_error());
$count = '1';
}
echo $count;
?>
|
|
|
As you can see, we added the variable $count to our code. This variable tells us the value of the table "count", or, if we're adding a new row, $count is set to 1. |
|
| |
| PHP: A simple MySQL search Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL | | | Beginners guide to PHP and MySQL - Creating a simple guest book Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL | | | Counting - Creating a GIF based counter using PHP and MySQL Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL | | | Counting - Creating a more sophisticated GIF based counter using PHP and MySQL Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL | | | Jump Start to Easy URLs Categories : PHP, Beginner Guides, MySQL, File System, To PHP | | | Who's Linking? Categories : PHP, Beginner Guides, To PHP | | | Grabb'n and Pars'n Categories : Beginner Guides, PHP, To PHP | | | Start Using MySQL Categories : MySQL, Databases, To MySQL, Beginner Guides | | | Beginners Guide to PHP - Introduction to cookies Categories : Beginner Guides, Cookies, To PHP, PHP | | | How To add paging (Pagination) with PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | How TO Install PHP, Apache and MySQL on Linux or Unix Categories : PHP, MySQL, Apache, Installation, Beginner Guides | | | Creating an IE-Only Database Driven Menu System With PHP, MySQL and DHTML Categories : PHP, MySQL, Databases, DHTML | | | PHP 101 Part 10 of 15 : A Session In The Cookie Jar Categories : PHP, Beginner Guides, Cookies, Sessions | | | PHP, MySQL and Authentication 101 Categories : PHP, Databases, MySQL, Authentication | |
| | | | Anonymous wrote : 3 Nice Article. | | | Anonymous wrote : 9
I want to know how we can do Error Validation in the
php without using javascript
| |
|
|
|