Archive for July 13th, 2010

July 13th, 2010

Self-taught Php/mysql: a Simple Page Counter Tutorial

self-taught PHP / MySQL: a simple Page Counter

This article is a brief introduction to PHP and MySQL to counter the example of a simple page. I will demonstrate creating database in MySQL, connect to the database from a PHP script that queries the database for information, the display of information in a Web page and writing the information into the database back. As always is the fastest way to master the process jump right into the code, look over and use it. We make a MySQL database to store the page name and increase the number of page views and use of PHP, and shows the amount of on a website. First, find all the Code , and then I’ll go over it in detail:

This goes in a file called “PageCounter. Php”

You will have noticed no doubt that the script “as” another, so without further delay here is the “connect. Php” file:

< br />
The PageCounter. PHP script requires a database operation. Only briefly, here’s how to create it.

create the database:

Log in to your MySQL Unix prompt (perhaps the #):

# Mysql-uYourUserName pYourPassword

On the MySQL prompt, enter the following commands:

mysql> create database pages;

mysql> USE sides;

mysql> CREATE TABLE counter (pagename varchar (60), hits int, stamp timestamp);

mysql> quit;

Of course, you can search the database and table with the utility companies or web-based interfaces, but it seems easier to just type the three commands?

If you just wanted some code for a simple counter, which is all you need. Put this text into an SHTML web page or this text to a PHP web page, copy the above two files into the same directory, and you’re done.

The first thing you’ll notice about the scripts have the dollar sign ($) are. All variables in PHP scripts start with a $. Everything begins with an $ is a variable. Instructions – the instructions and functions – with a semicolon (;). launches the script, and it ends with. If your script is hosed down, look at them first.

details about the PageCounter. PHP Script

Line 1

include_once to connect. “Php”;

The first line in the script is exactly what he seems. It includes everything to connect to the file. “Php”. The “_once” means that it only once, included even if the line twice in the script. The reason why I have separated him is that the connection to the order of the database. All PHP / MySQL scripts, there is need, it is always the same, one can only contain him and you use the function.

Line 2 pagename

$ = $ _SERVER ["REQUEST_URI "];

The next line creates a variable called $ pagename and sets the value to a special predefined variable $ _SERVER ["REQUEST_URI"]. The square brackets [] are used arrays in PHP. $ _SERVER Is a predefined set of headers and hiking trails. This particular one is the name of the file that accesses the script, ie, the name of the page that the counter on

Line 3 result

$ = mysql_query (“SELECT * from counter where pagename = ‘$ pagename’”);

;
All work is the database with mysql_query, which sends an SQL command string MySQL done after you have logged in and connected to the database to connect to. PHP script. “Select * from counter where pagename = ‘$ pagename,” $ pagename replaced with its value. But there is one quirk – the single quotes must be inside the double quotes. If I had it the other way around, with the single quotation marks outside, would the search for the text “$ pagename be” instead of the value. $ Result set is the result. It can not name names, but in the tutorial scripts it will always result $, so it is here.

Line 4

if (mysql_num_rows ($ result) == 0) (

The fourth line of the PHP version of “if-then”. We just verify if there are any results from the query in line 3 The syntax is representative of PHP programming in general so it is a good place to start. to group the curly brackets () are statements. The curly brackets () are used for the “if” condition. All are within the curly brackets, if the “if” condition is true, are executed. PHP uses the double equals sign == to compare, if I had only used a single equal sign would try it, mysql_num_rows set to 0, which would not work for our purposes. A missing equals sign is the second thing, because if your script is hosed down and look it is not missing a $ ‘or.

Line 5

mysql_query (“INSERT INTO counters (pagename, hits) pagename values (‘$’, ’0′)”);)
;
Within the brackets, which only happens if no records on line 4 of the page, looking for notes for which we, the statement adds a new record with the site’s name and zero for the number of hits. Although mysql_query is a feature that is not necessarily a variable result = $ before. It is optional in PHP, if you do not care about the return value.

The closing curly brace) from the “if” statement is here, as we only needed to make a statement about our record.

Line count 6

$ result = mysql_result ($, 0, “hits”);

mysql_result the current data fetches from the result set. Enter the result set (of mysql_query), the line number (0), and the name of the column (“hits”). This is a little confusing at first since they took us four steps: 1) Log on to MySQL, 2) a connection to the database, 3) Select data from the table, and 4) Returns a specific part of data. Putting the repetitive first two steps in an include file, where you more or less about it, it makes intuitive: The SQL data with mysql_query select password, and then retrieve data mysql_result.

Line 7

$ count = $ count + 1;

Just add the counter variable. This is the number of hits the page requesting the script.

Line 8

mysql_query (“update counter set hits = $ count where pagename = ‘$ pagename’”);

;
As with line 5, we will send an SQL command directly to MySQL. This updates the count only the right side, the variable $ pagename.

Line 9

echo “Page Count:”. $ Count;

The echo function writes text on a web page, in this case the text “Page Count:” followed by what value is in $ count. The time in between is the PHP connection operator: it simply adds together the two strings. Echo sees it as a string and returns it.

details to enter on the connection. PHP script:

All this script does is connect to the MySQL server and select the database.

Line 1

$ host = “localhost” $ user = “yourusername” $ password = “YourPassword” $ dbase = “pages”;

These are the inputs for the connection and select_db functions. Of course, you can set the value in the functions on the line 3 and 4 and remove this line, but it is easier to change later (if you use this code again for example), if you just list them out at the top. The host and dBase not need to be changed in this example. The user and password are specific to your MySQL setup. As shown here, you can put as many statements that care on a line as you want, not PHP.

Line 2

/ / change the username and password for your MySQL username and password

The double slashes / / denote a comment line which is ignored by PHP. Each comment line must slashes.

Line connect 3

$ = mysql_connect ($ host, $ user, $ password);

Log into your MySQL mysql_connect command. They would host “localhost” Change to the database server if you have access to MySQL from another server, provided you have set the access rights for the specified user /


Line 4

mysql_select_db ($ dbase, $ connect);

Since we can have several databases in MySQL server, we have to choose one before sending SQL statements to it. As I mentioned earlier, this part is repetitive, and once it is in this file and working conditions can you forget it.

In this tutorial, we have a web simple but functional page counter implemented with PHP / MySQL investigated. We examined the basic syntax of PHP statements and variables that PHP “include” function and “if” function in control, and basic PHP MySQL functions mysql_connect mysql_select_db, mysql_query, mysql_num_rows mysql_result and. For further reference, the reader should bookmark http://dev. MySQL. com/doc/refman/6. 0/en/index. html and http://us. php. net / manual / en / funcref. php.

Bill Hamilton is a former database administrator for United News and Media, Inc. and VNU. He currently operates several PHP / MySQL driven websites including ; Gems and Beads


; Gems and Beads