Self-taught PHP / MySQL: a simple counter Page
This article is a brief introduction to PHP and MySQL using the example of a simple counter page. Let me illustrate the creation of the MySQL database, the connection to the database from a PHP script, consult the database of information, displaying information on a website and write the information in the database. As always, the fastest way to govern the process is to jump directly to the code, review and use it. We will make a MySQL database to store the page name and the number of page views and use PHP to show the increase and have a website. First of all here is all the code, and then go into detail:
This goes in a file called "PageCounter. php "
Do not hesitate to realize that the script of "include" another, so without further ado, here is the "connection. php "
The PageCounter. php script requires a database to work. In short, here's how to create it. Creating the database:
Log in to MySQL on Unix (which could be #):
# Mysql-uYourUserName-pYourPassword
In MySQL type the following commands:
mysql> create database pages;
mysql> pages use;
mysql> create table counter (pagename varchar (60), hits int, stamp timestamp);
mysql> quit;
Of course, you can create the database and the table with public services or Web-based interfaces, but not simple enough to insert three commands?
If you only want a code for a simple counter, which is more than enough. Put this text in a web page SHTML, or this text to a PHP web page, copy these two files in that directory, and you're done.
The first thing you'll notice that the scripts are the dollar signs ($). All variables in PHP start with a $. Everything starts with a $ is a variable. Statements – the statements and functions – end with a semicolon (;). The script begins and ends. When the script is a tube, try this first. Details PageCounter. php script
Line 1 include_once "connect. Php ";
The first line in the script is what it seems. It includes everything in the file "link. php ". The _once "means that only included once, even if he had the line twice in the script. The reason is that everything that has separated the material connection to the database. All php / MySQL scripts need, which is always the same, so you can only put on their own and use included.
_SERVER Line 2 pagename = $ $ [ "REQUEST_URI"];
The next line creates a variable called $ page name and set the value of a special pre-defined variable $ _SERVER [ "REQUEST_URI"]. Brackets [] are used for arrays in PHP. $ _SERVER Is an array of default headers and roads. This especially is the name of the file that has access to the script, I. e. , The name of the page the counter is in
Line 3 $ result = mysql_query ( "SELECT * from counter where pagename = '$ pagename");
All work is done with database mysql_query, which sends a command string SQL to MySQL, having already registered and connected to the database with the connection. PHP script. "Select * from counter where pagename = '$ pagename" $ pagename replaced with its value. But there is a specialty here – the single quotes must be enclosed in double quotes. If I had back with the citation to be unique, the request would be that the text "$ pagename" instead of value. $ Result is the result set. Can be any name, but in the script tutorial is always $ result, so it is here.
4in Line (mysql_num_rows ($ result) == 0) (
The fourth line is the PHP version of "if-then". Simply checks if there is any query result in line 3. The syntax is the representative of PHP code, in general, so it's a good starting point. The keys () are used for group instruction. The sections of curves () are used for the condition "if". Everything inside the braces is executed if the "if" condition is true. PHP uses the double equal == for comparison, if I had used a single equal sign mysql_num_rows would try to set to 0, which would not work for our purposes. An equal sign is missing is the second thing to look for when the script is a tube and not without a $ o;.
5mysql_query Line ( "INSERT INTO counter (pagename, hits) VALUES ( '$ pagename', '0')");)
Inside the brackets, which occurs only when the line 4 did not find any record of the page we have tried, the statement creates a new record with the site name and zero to the number of steps. Mysql_query even if it is a function, not necessarily need a variable $ result = front of him. This is optional in PHP, if you do not mind the return value.
The close curly bracket), the "if" is here, as being necessary only to create our registration statement.
View line 6 $ = mysql_result ($ result, 0, "hits");
mysql_result retrieves the actual data in the result set. Specifies the result set (mysql_query), the line number (0), and the name of the column ( "hits"). This is a bit 'of confusion at first because here he had found four stages: 1) access to MySQL, 2) connect to the database, 3) Identify the table, and 4) for a particular piece of data. Put the first two phases of a repeated include file where you can more or less forgotten makes them more intuitive to use SQL to select data with mysql_query, then mysql_result data recovery.
Line 7 $ count = $ count + 1;
Just add one to the variable count. This is the number of page views the page that calls the script.
8mysql_query Line ( "update hits counter set = $ count where pagename = '$ pagename");
As with the line 5 to send a SQL command directly to MySQL. This page is updated only count for the adequacy of the variable $ page.
9echo Line "Pages." $ Count;
The echo function writes the text of a website, in this case, the text "Page" followed by any value in $ count. The interim period is the concatenation operator PHP: just add the two strings together. Echo sees it as a string and outputs it. Details about the connection. PHP script:
All this script does is connect to MySQL server and select database.
Host Line $ 1 user = "localhost", $ = "username" $ password = "YourPassword", $ Database = "Pages";
These are the inputs for the connection and functions select_db. Of course, you can enter values in the functions of line 3 and 4 and remove this line, but it is easier to change later (when you re-use this code, for example), if only the list above. The host and dBase should not be modified in this example. The username and password are specific to the configuration of MySQL. As shown below, you can put the statements that you want in a line, PHP does not care.
Line 2 / / change the username and password for your username and password for MySQL
Bars double / / denote a comment line that is ignored by PHP. Each comment line needs of the bar.
Line $ 3 connect = mysql_connect ($ host, $ user, $ password);
Log in to your MySQL with mysql_connect command. You may change the place of "localhost" for the MySQL database server, if access from another server, provided you have set the access rights for the specified user /
4mysql_select_db Line ($ connection dBASE, $);
Since we are able to have multiple databases on the MySQL server, we must select one before sending SQL commands to it. As mentioned earlier, this part is repetitive, and once in this file and work can be forgotten.
In this tutorial we looked at a simple but functional website counter PHP / MySQL. We reviewed the basic syntax of states and variables in PHP, the PHP "include" and "if" function to control the basic functions of PHP and MySQL mysql_connect, mysql_select_db, mysql_query, mysql_num_rows and mysql_result. For reference, the reader should http://dev marker. 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. It currently manages many php / mysql driven websites including precious stones and pearls