| Image-recognition Software Lets Computers 'See' Like People It takes surprisingly few pixels of information to be able to identify the subject of an image, a team led by an MIT researcher has found. The discovery could lead to great advances in the automated identification of online images and, ultimately, pr... |
| Create a simple hit counter using PHP and MySQL |
|
|
|
In this article I describe how to use PHP and MySQL to produce a simple counter that can be placed on a web page. PHP and MySQL work very well together, and this article shows, hopefully, how easy they are to use to produce a useful little utility. In order for the counter to work, the web server you upload the files to needs to support PHP and MySQL. Most good hosting solutions do. You can download the various scripts used to produce the counter from the following web address:http://www.computernostalgia.net/counter/counter.zip. The scripts are also listed below. The counter needs a database called 'counter', a table in that database called 'countertable', and a field in the table called 'count'. If you want to use a different database, table, or field name, make sure you change the appropriate references to these names in the scripts. Files The zip file (counter.zip) contains the following files:
Note that for display considerations, in the following listings, opening and closing angle brackets for tag names ('<..>') are replaced by opening and closing square brackets ('[..]'). create_database.php This script creates a MySQL database called 'counter'. Upload this script to your web server and run it first to create the database. [html][head][title]Create MySQL Database[/title][/head] //This script creates a database on the MySQL server. //Connect to MySQL server //If you need to supply a username and password, then use the following line //If the connection cannot be made, display an error message //Create a database called counter //Close the connection to the MySQL server create_table.php This script creates a table (countertable) in the counter database. The table has one field, called 'count', which can store an eight digit number. This allows a counter value up to 99,999,999. Upload this and run it once the database has been created. [html][head][title]Create Table in Database[/title][/head] //This script creates a table (countertable) in the database (counter). //Assign the name of the database (counter) to the variable $db. //Connect to MySQL server. //If you need to supply a username and password, then use the following line //If the connection cannot be made, display an error message. //Select the database. If the database cannot be selected, display an error message. //Create a table called countertable in the database. //Close connection to MySQL server. ?] reset_counter.php This script sets/resets the counter to zero. Upload this and run it to initialise the counter to zero. You can run it at any time to reset the counter to zero. [html][head][title]Reset Counter[/title][/head] [?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 if (! $link) die("Cannot connect to MySQL"); // Set the counter to zero //close link to MySQL server [/body] counter.php This is the actual counter. The code in this file should be pasted into the web page that will contain the counter (or it can be run on its own). This web page, which will typically be part of a web site, must have a .php file extension, otherwise the PHP code will be ignored by the web server. [html][head][title]Increment Counter[/title][/head] [comment] [?php //Set database to counter //connect to server and database //If you need to supply a username and password, then use the following line if (! $link) die("Cannot connect to MySQL"); //Increment counter //extract count from database table //Display counter. If you want to change the appearance of the counter, edit //close link to MySQL server [/body] That's it!
|
||
| < Prev | Next > |
|---|

