Self-taught PHP / MySQL: a simple page counter This article is a brief introduction to PHP and MySQL, using the example of a simple page counter. I want to show the creation of the MySQL database, the connection to the database from the PHP script that queries the database for information, display information in a Web page, and write information in database. As always, the fastest way to advance the process with the master code to use to look through and then. We’ll save a MySQL database on the name of the page and make the number of page views and increase the use of PHP, and shows the importance of a web page. Most important 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 includes another, without delay, here is the “link to connect. “php file: The PageCounter. PHP script requires a database to work. Briefly, here’s how they create. Creating the database: Log in to your Unix MySQL prompt (maybe #): # Mysql uYourUserName-pYourPassword At the mysql prompt, type these commands: mysql> create database pages; mysql> use sites; mysql> create table counter (pagename varchar (60), hits, int timestamp buffer); mysql> quit; Of course, you can search the database and table with the utility companies or Web interfaces, but it seems easier to enter only three levels? If you have only one code for a simple wanted to meet, that’s all you need. Put this text into 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, the dollar sign ($). All variables in PHP start with a $. It starts with a $ is a variable. Instructions – Instructions and functions – with a semicolon (;). launches the script, and it ends with. If your script is sprinkled, take a look at this first. Details on PageCounter. PHP script Line 1 include_once “get in touch. Php”; The first line in the script is exactly what it seems. It includes what to enter in the file “link. Php. The _once “means that it included only once, even if you had the line twice in the script. The reason I separate the need to connect to the database. All PHP / MySQL in need is always the same, so you can use yourself and behave civil. Line 2 $ pagename = $ _SERVER [ "REQUEST_URI"]; The next line creates a variable named $ pagename and sets the value to a special pre-variable $ _SERVER [ "REQUEST_URI"]. The brackets [] are used with arrays in PHP. $ _SERVER Is a predefined set of headers and hiking trails. This is one of the name of the file, access to the script, namely the name of the page, the counter on Line 3 $ result = mysql_query ( “SELECT * FROM bar where pagename = ‘$ pagename’”); All work is done, the database with mysql_query, which sends an SQL command string in MySQL after you have already registered and the database is connected with. Php script. “Select * from counter where pagename = ‘$ pagename,” $ pagename replaced by its value. But there is a trend here – the single quotes must be inside double quotes. If I do the reverse, with the single quotation marks outside, which would find the text “$ pagename” instead of value. $ Result Result Set. There can be no names, but in the tutorial scripts is still $ result, so it is here. Line 4Si (mysql_num_rows ($ result) == 0) ( The fourth line is the PHP version of “if-then”. It only checks to see if the query results in line 3 The syntax is representative of the PHP programming in general there is a good place to start. The brackets () are designed for use instructions. The brackets () are used for the “if” condition. Everything inside the braces is executed when the “if” condition is met. PHP uses the double equals sign == to compare, if I were to define a single equal sign mysql_num_rows tries to 0, which does not work for our purposes was used. A missing equals sign is the second point to consider whether your script is watered, and it is not a lack or $. Line 5mysql_query ( “INSERT INTO counter (pagename, hits) VALUES ( ‘$ pagename’, ’0′)”);) Between the parentheses, which sought only line 4 no documentation on the site we found, the statement creates a new record with the site name and zero for the number of hits. Although mysql_query is a function, it is not necessarily a variable $ result = front. PHP is optional if you do not worry about the return value. The closing brace) of the “if” comes here because we are a declaration required to build our balance sheet. Line 6 $ count = mysql_result ($ result, 0, “hits”); mysql_result retrieves data from the current result set. Enter the result set (mysql_query), the line number (0), and the name of the column ( “hits”). It’s a bit confusing at first because you come home we took four steps: 1) in MySQL, 2 log) to connect to the database 3) Select the table data, and 4) provides specific portion of the data. Put the first two steps in a repetitive file included when you more or less forgotten, it is intuitive: SQL to select data with mysql_query and click on it to retrieve data mysql_result. Line 7 $ count = $ count + 1; How to add a counter variable. The number of page views on the application page of the script. Line 8mysql_query ( “update counter set hits = $ count, where pagename = ‘$ pagename’”); As the line 5, we send an SQL command directly to MySQL. This update includes only the right side, the variable $ pagename. Line 9echo “Page Count:”. $ Count; The echo function written text on a web page number in this case, the text “Page Count:” followed by a dollar value. The time between the two is the operator connecting PHP: it simply adds together the two chains. Echo sees it as a string and they are there. To enter the connection details. PHP script: All this script does that connect to MySQL and select database. Line 1 $ host = “localhost”, $ user = “YourUserName” $ password = “YourPassword” $ base = “Pages”; These are the entries for the contact and functions select_db. Of course, you can insert values into functions on the line 3-4 and change remove this line, but it is much easier at a later date (when you re-use this code, for example), if n ‘have such a timetable at the top. The host and dBase must not be modified in this example. The user and password are unique to your MySQL configuration. As shown here, you can add as many statements that write a line you like, PHP does not care. Line 2 / / Change the username and password to your username and password MySQL The double slash / / means a comment line is ignored by PHP. Each comment line must have the slashes. Line 3 $ connect = mysql_connect ($ host, $ username, $ password); You are at your command mysql_connect MySQL. They are host to change “localhost” on the server database if you have access to MySQL from another server in place when the permissions for the user specified / Line 4mysql_select_db ($ dbase, $ connect); As we have several databases in MySQL, we need before SQL statements to select it. As I mentioned earlier, this part is new, and once in this file and working conditions, you can forget it. In this tutorial, we discussed a simple website but functional implements Counter with PHP / MySQL. We reviewed the basic syntax of variable declarations PHP and PHP “include” function and “if” function control, and basic PHP MySQL mysql_connect mysql_select_db, mysql_query, mysql_num_rows and mysql_result. For 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 the United News & Media, Inc. and VNU. It currently operates a number of PHP / MySQL Driven Websites and precious stones and pearls

Related posts:

  1. Self-taught PHP / MySQL: a simple page counter Tutorial
  2. Self-taught PHP / MySQL: An introduction to the page counter
  3. Creating a simple Facebook Application PHP tutorial