
This article explains how to use PHP and MySQL to produce a simple counter that can be placed on its website. PHP and MySQL work well together, and demonstrates the this section, we hope, how easily you can design a useful little utility.
To counter these works, the server URL that loads the file to support the needs of PHP and MySQL. Most solutions do well.
The meter needs a database called "cons" table in the database called "countertable, and in table named" count ". To use another database, table or field name sure to change the references to names in scripts.
Archives
The zip (counter.zip) contains following files:
- create_database.php
- create_table.php
- reset_counter.php
- counter.php
Note that for the circumstances demonstrate, on the list below, the opening and closing brackets of tag names ('') instead of opening and closing brackets ('[..]').
create_database.php
The script creates a database MySQL database called "cons-revolution." Download the script on your server and network during the first, to create the basis data.
[HTML] [] A [title] MySQL CREATE DATABASE [/ title] [/ head]
[body]
[? php?
/ Creates / This script database data to the MySQL server.
/ / The name of the database is accessible without any data.
/ / Connect to server MySQL
$ Link = mysql_connect ( "localhost");
/ / If you must provide your username and password, then use the line following the
/ / As the above code in place to replace the username and password correctly.
/ / $ Link = mysql_connect ( "Localhost" "username", "password");
/ / If the connection can not be done, displaying an error message
if ($ link)
death ( "can not connect to MySQL");
/ / Create a database called war against
mysql_create_db ( "counter") or die ( "Error:". mysql_error ());
/ / Close the connection to MySQL server
mysql_close ($ link);
?]
[/ Body]
[/ HTML]
create_table.php
The script creates a table (countertable) on database meter. The table has a field called "count", which may be the number of stores to eight digits. This allows the counter value up to 99,999,999. Download it and when in this database is created.
[HTML] [] a [title] Create a table in database [/ title] [/ head]
[body]
[? php?
/ Creates / This table script (countertable) on the database (meter).
/ / Add the name of the database (OTC) for the variables $ DB.
$ DB = "counter";
/ / Connect to MySQL Server.
$ Link = mysql_connect ( "localhost");
/ / If you provide your username and password password, then use the following line
/ / As the above code in place to replace the username and password correctly.
/ / $ Link = mysql_connect ( "localhost", "username", "password");
/ / If the connection can not make that mistake.
if ($ link)
death (can not "connect to MySQL");
/ / Select the database. If you can not select a database, it shows error message.
mysql_select_db ($ db, $ link)
or die ( "Select DB Error:". mysql_error ());
/ / Create a table called countertable in the database.
/ East / table in the field: count, which should allow visitors 99999999
( "CREATE TABLE INT mysql_query (countertable count (8))") or die ( "Create table Error:". Mysql_error ());
/ / Close the connection to MySQL server.
mysql_close ($ link);
?]
[/ Body]
[/ HTML]
reset_counter.php
The script sets / resets the counter to zero. Load and run it to initialize the counter to zero. It can run at any time to reset the counter to zero.
[HTML] [] a [title] Hit reset [/ title] [/ head]
[body]
[? php?
/ / Point your browser on this page to set or reset a counter to zero.
$ DB = "counter";
$ Link = mysql_connect ( "localhost");
/ / If you must provide your username and password, then use the next line
/ / The code instead of replacing the username and password correctly.
/ / $ Link = mysql_connect ( "localhost" "username", "password");
if ($ link) death ( "can not connect to MySQL");
mysql_select_db (DB $ Link, $) or die ( "can not open $ DB:". Mysql_error ());
/ / Set the counter to zero
(Insert countertable I mysql_query (number) VALUES ('0')");
/ / Close the connection to MySQL server
mysql_close ($ link);
?]
[/ Body]
[/ HTML]
counter.php
This is the real counter. The code must be pasted on the file containing the location of the recipe (or you can run on their own). This site, which is normally part of the site, you must have a file extension. php, Otherwise the PHP code will be ignored by the web server.
[HTML] [] a [title] Increment Hit [/ title] [/ Head]
[body]
[Review]
Include all comment below (bottom closing tag of the body) in the page
Where do you reset the counter.
[/ Comment]
[? php?
/ / Database intended to resolve
$ DB = "counter";
/ / Connect to server and database
$ Link = mysql_connect ( "localhost");
/ / If necessary, you provide your username and password, then use the following line of
/ / As the above code in place, to be replaced The username and password correctly.
/ / $ Link = mysql_connect ( "localhost", "username", "password");
if ($ link) die ( "can not be connect to MySQL ");
mysql_select_db (DB $ link, $) or die ( "No Open the $ DB:". mysql_error ());
/ / Increment Counter
( "Update count mysql_query countertable SET count = 1");
/ / Extract to have the database table
$ Number = mysql_query ( "SELECT * FROM countertable");
/ / Queries on the screen. To change the appearance of the bar, organize
/ / The following table and the source configuration.
print "[table border = 1 cellpadding = 3 cellspacing = 0 width = 80];
and ($ get_Count = Mysql_fetch_array ($ count)) (
Print "[tr]";
foreach ($ get_Count as $ field)
print "[td align = right] [font>
print "[/ tr]";
[Print / table] ";
)
/ / Close the connection to MySQL server
mysql_close ($ Link);
?]
[/ Body]
[/ HTML]
Voila!
PHP Tutorial: Using Eclipse+PDT as a PHP IDE (Part 2 of 2)