PHP/MySQL 3-3 (1) _ PHP Tutorial

Source: Internet
Author: User
PHPMySQL 3-3 (1 ). 1. welcome to the third and last course of this tutorial. If you have learned the first and second lessons, you have mastered the basic functions for installing and programming MySQL and PHP.

Welcome to the third and last course of this tutorial. If you have learned the first and second lessons, you have mastered the basic installation and programming knowledge of MySQL and PHP. Next we will introduce some other PHP functions, which may be useful to you and make your development process easier. First, let's look at the header file.

Do you know some basic concepts of header files? The header file is an external file whose content is included in the main program. The method is also very simple: reference the header file name in the program file, this header file will be included. Using the header file in PHP involves two functions: include () and require (). These two functions have very little difference, but they are very important. so we need to study them carefully. The require () function works in a similar way as XSSI. no matter which part of the program uses this function, only the program starts to run, the header file content is processed as part of the program itself. Therefore, if you use the require () function in a condition determination statement, the header file will be included even if the condition is not true.

The include () function only includes the header file content when this statement is executed. If the program does not run here, PHP will not care about it. This means that when you use include in the condition determination part, it will work exactly as you want.

In addition, if you use the require () function and the header file you specified does not exist, the program stops running and produces errors. If you use include (), the program generates a warning message, but continues to run. You can try it yourself, run the following program, replace include () with require (), and then compare the results of the two programs.

$ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;



$ #@ 60 ;? Php

Include ("emptyfile. inc ");

Echo "Hello World ";

? $ #@ 62;



$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

I like to name the header file as. inc, so that the header file can be distinguished from general programs. If you do the same, modify the configuration file of the Web server software so that it can process the. inc file as a php file. Otherwise, hackers may guess your header file name and display the header file content in plain text in a browser. At this time, if your header file contains some confidential information (such as database passwords), it will be terrible. <

So what do you do with header files? Very easy! Put the content that is common to all programs in the header file. Like the HTML file header, footer, database connection code, and some functions you have defined. Copy the following text to a file and save it as header. inc.

  $ #@ 60 ;? Php

$ Db = mysql_connect ("localhost", "root ");

Mysql_select_db ("mydb", $ db );

? $ #@ 62;

$ # @ 60; html $ # @ 62;

$ # @ 60; head $ # @ 62;

$ # @ 60; title $ # @ 62;

$ #@ 60 ;? Php echo $ title? $ #@ 62;

$ # @ 60;/title $ # @ 62;

$ # @ 60;/head $ # @ 62;

$ # @ 60; body $ # @ 62;

$ # @ 60; center $ # @ 62; $ # @ 60; h2 $ # @ 62; $ # @ 60 ;? Php echo $ title? $ # @ 62; $ # @ 60;/h2 $ # @ 62; $ # @ 60;/center $ # @ 62;

After that, create another file named footer.txt, which can contain some text and tags used at the end of the program.

Now let's create another file, which contains the real PHP program code. Try the following code. of course, make sure that the MySQL database server is running.

  $ #@ 60 ;? Php

$ Title = "Hello World ";

Include ("header. inc ");

$ Result = mysql_query ("SELECT * FROM employees", $ db );

Echo "$ # @ 60; table border = 1 $ # @ 62 ;";


Echo "$ # @ 60; tr $ # @ 62; $ # @ 60; td $ # @ 62; NAME $ # @ 60;/td $ # @ 62; $ # @ 60; td $ # @ 62; position $ # @ 60;/tr $ # @ 62 ;";

While ($ myrow = mysql_fetch_row ($ result )){

Printf ("$ # @ 60; tr $ # @ 62; $ # @ 60; td $ # @ 62; % s $ # @ 60; /td $ # @ 62; $ # @ 60; td $ # @ 62; % s $ # @ 60;/tr $ # @ 62; ", $ myrow [1], $ myrow [2], $ myrow [3]);

}

Echo "$ # @ 60;/table $ # @ 62 ;";

Include ("footer. inc ");

? $ #@ 62;

See what happened? The content in the header file is merged into the program, and PHP executes all the code. Note how $ title is defined before the header file contains header. inc. The code in header. inc can access its value. In this way, the webpage title is changed. Now you can use the header file header of header. inc in any program. all you need to do is to get a proper value for the $ title variable in each main program.

Header files, HTML, condition-based judgment statements, and cyclic statements. when added, you can use the most concise code to write various complex programs with different functions. When using the function at the same time, the header file can make full use of it, which we will see later.

Next, we will introduce the wonderful part: data verification.

II. data verification

Imagine this situation: we have designed all the databases properly. now we ask the user to enter the information to write it to the database. Assume that you have a field that requires numeric type information, such as price, while a cute user enters text information in this column, the execution process of your application fails. MySQL databases refuse to accept the text data you provide in SQL statements and submit a "solemn protest" to you ".

What should we do? You must use data verification to prevent the above situations.

To put it simply, data verification refers to checking the data (usually transmitted through HTML tables) to see if it complies with certain rules. Rules can be diverse. for example, a data element cannot be empty, or the content of a data item must meet certain requirements (for example, in the preceding example, the content must be a number rather than a text, or the email address must contain a "@" character ).

Data verification can be performed at either the server or the client. PHP is used for data verification at the server end, while JavaScript/"target =" _ blank "> JavaScript or other client-side scripting programming languages can provide the client-side data verification function. This article is about PHP, so we will focus on server-side verification here. If you want to find some off-the-shelf data comparison programs running on the client, you can go to the monkey Library.

If you leave the database aside for the moment, let's talk about the data verification method of PHP. If you want to (or, if you want to record the data we want to verify), you can add other fields to the employee database created earlier, which is very simple, use the ALTER Statement of MySQL.

There are several PHP functions that can be used for data verification. Some are simple and some are complicated. Strlen () is a simple function that tells us the length of a variable.

More complex: ereg (). This function can process the complete regular expression for complex verification. I don't want to talk too deeply about regular expressions, because many books are dedicated to this issue. However, I will give some simple examples on the next page.

Let's start with a simple example. The following program checks whether a variable exists.

There are two pages in this news. Currently, there are two pages in page 1st.


  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

If ($ submit ){

If (! $ First |! $ Last ){


$ Error = "Sorry, you must enter all columns! ";
} Else {

// Process table input

Echo "thank you! ";

}
}

If (! $ Submit | $ error ){

Echo $ error;

? $ #@ 62;

$ # @ 60; P $ # @ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PHP_SELF? $ # @ 62; "$ # @ 62;


Column 1: $ # @ 60; input type = "text" name = "name" value = "$ # @ 60 ;? Php echo $ first? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;


Column 2: $ # @ 60; input type = "text" name = "surname" value = "$ # @ 60 ;? Php echo $ last? $ # @ 62; "$ # @ 62; $ # @ 60; br $ # @ 62;

$ # @ 60; input type = "Submit" name = "submit" value = "input information" $ # @ 62;

$ # @ 60;/form $ # @ 62;

$ #@ 60 ;? Php


} // If ends

? $ #@ 62;



$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

The key part of this program is the nested condition judgment statement. The first layer checks whether the user has pressed the send data button. If yes, the program then checks whether both the $ first and $ last variables exist. That | the symbol "or", and! It indicates "not ". The program uses

Welcome to the third and last course of this tutorial. If you have learned the first and second lessons, you have mastered the basics of installing and programming MySQL and PHP...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.