Php/mysql 3rd Pass-the third day (i) _php tutorial

Source: Internet
Author: User
First, the basic function

Welcome to the third and final lesson of this tutorial. If you have already learned the first and second lessons, you have mastered the basics of installation and programming for MySQL and PHP. Let's introduce some of the other PHP functions that might be useful to you and make your development process easier. First, let's look at the document.

You should know some basic concepts of the header file, right? The header file is an external file whose contents are included in the main program. The method is also very simple: refer to the header file name in the program file, the header will be included in it. Using a header file in PHP involves two functions: include () and require (). The difference between these two functions is very small, but very important, so we have to study it carefully. The Require () function works similarly to Xssi, and the contents of the header file are handled as part of the program itself, regardless of the part of the program that uses the function, only when the program starts running. Therefore, if you use the Require () function in a conditional decision statement, the header file is included even if the condition is not true.

The Include () function only contains the contents of the head file when it executes to this statement. If the program does not run here, then PHP will not control it. This means that when you use include in the Conditional decision section, it will work exactly as you want.

Also, if you use the Require () function and the header file you specified does not exist, the program will stop running and produce an error. If you use include (), the program generates a warning message, but it continues to run. You can try it yourself, run the following program, then replace the include () with require () and compare the results of the two program runs.

$#@60;html$#@62;

$#@60;body$#@62;



$#@60;? Php

Include ("Emptyfile.inc");

echo "Hello World";

? $#@62;



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

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

I like the suffix of my head file as. Inc, so that you can separate your head files from the usual programs. If you do the same, modify the Web server Software configuration file so that it can handle the. inc file as a PHP file. Otherwise, hackers might guess your header file name, and then use your browser to display the contents of your head files in plain text format. At this point, if you have some confidential information (such as a database password) in your header file, that's bad. <

So, what do you do with your header file? Very simple! Put the content that is common to all programs in the header file. Like HTML file header, footnotes, database connection code, and some of your own defined functions or something. 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;

Then create another file with the name Footer.txt, which can contain some text and tags used at the end of the program.

Now, let's create a file with the real PHP program code inside. Try the following code, of course, you want to confirm 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;";-->
EC Ho "$#@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%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's going on here? The contents of the header file are incorporated into the program, and PHP executes all the code once. Note how $title is defined before it contains the Header.inc header file. The code in HEADER.INC can access its value. In this way, the title of the page is changed. Now you can use the Header.inc header file in any program, all you have to do is take a suitable value for the $title variable in each main program.

header files, HTML, conditional judgments, and looping statements, which add up to some, you can use the most concise code to write various complex programs with different functions. When used in conjunction with a function, the header file will be more effective, as we'll see later.

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

Second, the data check

Imagine the situation: we have designed the database, and now ask the user to enter information to write to the database. Suppose you have a field that requires a numeric type of information, such as a price, and a cute user enters text messages in this column, which causes your application to fail during execution. The MySQL database refuses to accept data about the type of text you provide in the SQL statement, and presents a "solemn protest" to you.

What do we do? You want to use data validation to prevent the above situation from occurring.

In a nutshell, data validation refers to the fact that we check the data (usually by the user via an HTML form) to see if it complies with certain rules. Rules can be varied, such as a data element cannot be empty, or require that the content of a data item must meet certain requirements (for example, the preceding example requires a number instead of text, or requires that the e-mail address must contain an "@" word, and so on).

Data validation can be done on either side of the server or on the client. PHP is used for data validation at one end of the server, while javascript/"target=" _blank ">javascript or other client-side scripting languages provide client-side data validation capabilities. This article is about PHP, so we're here to focus on server-side validation. If you want to find some out-of-the-box data that runs on the client, you can go to the Web Monkey Library.

Put aside the database for the time being, let's first say the PHP data check method. If you want to (or say, the data that we want to verify), you can add the other fields to the employee database that you built earlier, simply by using the MySQL ALTER statement.

There are several PHP functions that can be used for data validation, some are simple, some are more complex. where strlen () is a relatively simple function, it can tell us the length of a variable.

More complicated is ereg (), a function that can handle a complete general expression for complex validation. I don't want to speak too deeply about regular expressions, because many books are written specifically for this issue. But I'll give you some simple examples on the next page.

Let's start with a simple example. The following program checks to see if a variable exists.

This news a total of 2 pages, currently on the 1th page 1 2


  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? Php

if ($submit) {

if (! $first | |! $last) {


$error = "Sorry, you must fill in all the fields!" ";
} else {

Working with table entries

echo "Thank you!" ";

}
}

if (! $submit | | $error) {

Echo $error;

? $#@62;

$#@60; p$#@62;

$#@60;form method= "POST" action= "$#@60;? php echo $PHP _self? $#@62; " $#@62;


First column: $#@60;input type= "text" name= "name" value= "$#@60;? php echo $first? $#@62; " $#@62;$#@60;br$#@62;


Second column: $#@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 End

? $#@62;



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

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

The key point in this program is nested conditional decision statements. The first layer checks whether the user pressed the button that sent the data. If so, the program then checks whether the two variables of $first and $last exist. that | | The symbol means "or", and! The symbol indicates "non". The procedure uses a

http://www.bkjia.com/PHPjc/532610.html www.bkjia.com true http://www.bkjia.com/PHPjc/532610.html techarticle Basic functions Welcome to the third and final lesson of this tutorial. If you've already learned the first and second lessons, you've mastered the basics of MySQL and PHP installation and programming ...

  • 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.