Archive for April 18th, 2010
Draw Vector Heart Artwork: Adobe Illustrator Tutorial
Author: adminApr 18
Self-taught PHP / MySQL: An introduction to the page counter
Author: adminApr 18
Self-taught PHP / MySQL: a simple page counter
This article is a brief introduction to PHP and MySQL to counter the example of a single page. I will demonstrate the creation of databases in MySQL, connect to the database from a PHP script that queries the database for information, display information in a Web page and writing information in the database. As always it is the fastest way to master the jump right into the code, look over and use it. We make a MySQL database to store the name of the page and increase the number of page views and use of PHP, and shows how much a website. First, here is the entire code, and then I’ll go into more detail:
This goes in a file called “PageCounter. Php”
You’ve probably noticed that the script “and” one another, without further ado here is the log. “Php” file:
The PageCounter. PHP script requires a database operation. Briefly, here’s how to create it. Creating the database:
Log in to your MySQL UNIX prompt (the # may be):
# Mysql PYourPassword uYourUserName
On MySQL, type the following commands:
mysql> CREATE DATABASE pages;
sides use mysql>;
mysql> CREATE TABLE cons (pagename varchar (60), hits int timestamp buffer);
quit mysql>;
Of course, you can search the database and table with utility companies or Web interfaces, but it seems easier to type the three levels?
If you just wanted a bit of code for a simple counter, which is all it takes. Put this text in a web page or SHTML this text to a PHP web page, copy the two files above in the same directory, and you’re done.
The first thing you notice about the scripts have the dollar sign ($) are. All variables in PHP start with a $. Everything starts with a $ is a variable. Instructions – instructions and functions – with a semicolon (;). launches the script, and it ends with. If your script is watered, look first. Details PageCounter. PHP script
Include_once Line 1 to connect. “PHP”;
The first line in the script is exactly what it seems. It includes everything to connect to the file. “PHP”. The _once “means that only once, even if the line included twice in the script. The reason I separated him is that the connection to the order of the database. All PHP scripts / MySQL, it is necessary, it is always the same, we can contain it and you use the function.
Line pagename = $ 2 $ _SERVER ["REQUEST_URI"];
The next line creates a variable called $ pagename and sets the value of a predefined special variable $ _SERVER ["REQUEST_URI"]. The brackets [] are used arrays in PHP. $ _SERVER Is a predefined set of headers and hiking trails. This is the name of the file that accesses the script, namely that the meter is the name of the page
mysql_query Line 3 $ result = (“SELECT * FROM bar where pagename = ‘$ pagename”);
All work is the database with mysql_query, which sends a command string SQL MySQL Server after you have logged in and connected to the database to connect to. PHP script. “Select * from counter where pagename = ‘pagename $, pagename” $ replaced by its value. But there is one peculiarity – the single quotes must be inside double quotes. If I had it in the other direction, with the single quotes outside, pagename is looking for the text “$ is” instead of value. The result set is $ result. We can not name names, but in the tutorial scripts, it is still $ result is here.
Line 4Si (mysql_num_rows ($ result) == 0) (
The fourth line of the PHP version of “what-if”. We just check the results of the query syntax line 3 is representative of the PHP programming in general so it’s a good place to start . to combine the braces () are statements. Braces () are used for “if” condition. All are in the brackets, if the “if” condition is true, are executed. PHP uses the double equals sign = = to compare, if I used a single equal sign he would try mysql_num_rows 0, which does not work for our purposes. A lack equal sign is the second thing, because if your script is watered and watch this not miss $ “or.
5mysql_query Line (“insert into counter (pagename, hits) pagename values (‘$’, ’0′)”);)
Within the brackets, which happens only if no records on line 4 of the page, finding notes for which we, the statement adds a new record with the site name and zero for the number of hits. Although mysql_query is a feature that is not necessarily a variable $ result = it. It is optional in PHP, if you do not care about the return value.
The closing brace) of the “if” statement is here, as we only need to make a statement about our record.
Line 6 $ count = mysql_result ($ result, 0, “hits”);
mysql_result retrieves data from the current result set. Enter game results (mysql_query), the line number (0), and the name of the column (“hits”). It’s a bit confusing at first because they took four steps: 1) Connect to MySQL, 2) a connection to the database, 3) Select the table data, and 4) Returns specific part of the data. Put the first two repetitive steps in an include file, which you more or less about this, it is intuitive: The SQL data with password mysql_query Select, and then retrieve the data mysql_result.
Line 7 $ count = $ count + 1;
Simply add the counter variable. The number of visits the page of the script.
8mysql_query line (“cons update hits count set = $, where pagename = ‘$ pagename”);
As for line 5, we will send a SQL command directly to MySQL. This update to mention that the right side, the variable $ pagename.
9echo Line “number of pages:”. $ Counter;
The echo function written text on a web page, in this case, the text “Page Count:” followed by what the value is $ count. The time between the two is the operator connecting PHP: it simply adds together the two chains. Echo considers it as a string and returns it. to enter details about the connection. PHP script:
All this script does that connect to MySQL server and select database.
dbase Home Line $ 1 = “localhost” $ user = “yourusername” $ password = “YourPassword” $ = “pages”;
These are the inputs for the connection and functions select_db. Of course, you can set the value of duties 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 have drawn the list on the top. The host and dBase not need to be changed in this example. The user and password are specific to your MySQL installation. As shown here, you can put as much as the care instructions on a line that you want, not PHP.
Line 2 / / change the username and password to your username and password MySQL
The double slashes / / is a comment line is ignored by PHP. Each comment line must bar.
Line 3 $ connect = mysql_connect ($ host, $ user, $ password);
Log in to your MySQL mysql_connect command. They host “localhost” Change to the server database if you have access to MySQL from another server, provided you have set the permissions for the user specified /
4mysql_select_db Line ($ dBase, $ connect);
Since we can have multiple databases to MySQL, we must choose one before sending SQL statements to her. As I mentioned earlier, this part is repetitive, and once he is in this file and working conditions can not you forget it.
In this tutorial, we have a simple but functional website cons implemented with PHP / MySQL investigated. We reviewed the basic syntax of PHP statements and variables that PHP “include” and the “if” function control basic functions PHP MySQL mysql_connect mysql_select_db, mysql_query, mysql_num_rows and mysql_result. For more details, the reader should bookmark http://dev. MySQL. com/doc/refman/6. 0/en/index. html http://us. php. net / manual / en / funcref. php.
Bill Hamilton is a former database administrator for the United News & Media, Inc. and VNU. It currently operates several PHP / MySQL engine sites, including precious stones and pearls
Vector swirl, swoosh and flowers: Illustrator Tutorial
Author: adminApr 18
PHP Tutorial: Registration and reservation: E-mail activation (Part 3)
Author: adminApr 18
