WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder



Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL Picture not available
ScriptMakers.com
Date : 2000-02-27
Grade : 2 of 5 (graded 1 times)
Viewed : 19503
Search : More Articles by ScriptMakers.com
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


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.









Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
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
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To 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
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
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
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
Anonymous wrote : 3
Nice Article.
Anonymous wrote : 9

I want to know how we can do Error Validation in the
php without using javascript