Home  >  Article  >  Backend Development  >  PHP_MySQL Tutorial-Day 3 Basic Functions Page 1/2_PHP Tutorial

PHP_MySQL Tutorial-Day 3 Basic Functions Page 1/2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:11697browse

Page 1 Basic Functions
Welcome to the third and final lesson of this tutorial. If you have studied the first and second lessons, then you already have the basic knowledge of MySQL and PHP installation and programming. Below we are going to introduce some other functions of PHP that may be useful to you and make your development process easier. First let's take a look at the header file.
Everyone should know some basic concepts of header files, right? A header file is an external file whose contents are included into the main program. The method is also very simple: quote the header file name in the program file, and the header file will be included. Using header files in PHP involves two functions: include() and require(). The difference between these two functions is small, but important, so we need to study it carefully. The require() function works similarly to XSSI; no matter where in the program it is used, the contents of the header file are processed as part of the program itself as soon as the program starts running. Therefore, if you use the require() function in a conditional statement, the header file will be included even if the condition is not true.
The include() function only includes the contents of the header file 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 a conditional part, it will work exactly as you want it to.
Also, if you use the require() function and the header file you specify does not exist, the program will stop running and generate an error. If you use include(), the program will generate a warning message but will continue to run. You can try it yourself, run the following program, then replace include() with require(), and then compare the results of the two programs.

Copy code The code is as follows:



include("emptyfile.inc");
echo "Hello World";
?>



Web page production | website construction | data collection.
I like to name the suffix of the header file as .inc, so that the header file can be distinguished from ordinary programs. If you do the same, please 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 the name of your header file and then use the browser to display the contents of the header file in plain text format. At this time, it would be bad if there is some confidential information in your header file (such as database password, etc.).
So, what do you use header files for? Very simple! Put those things that are common to all programs into header files. Like HTML file headers, footnotes, database connection codes, and some functions you define yourself. Copy the following text to a file and save it as header.inc.
Copy code The code is as follows:

$db = mysql_connect("localhost", "root ");
mysql_select_db("mydb",$db);
?>


<br><?php echo $title ?> <br>



A very comprehensive PHP technology website, with quite a lot of articles and source code.
Then create another file named footer.txt. The file can contain some text and markup that are used at the end of the program.
Now, let’s create another file. This file contains the real PHP program code. Try the following code, of course, you need to confirm that the MySQL database server is running.
Copy code The code is as follows:

$title = "Hello World";
include("header.inc");
$result = mysql_query("SELECT * FROM employees",$db );
echo "n";
echo "n";
while ($myrow = mysql_fetch_row($result)) {
printf("n", $ myrow[1], $myrow[2], $myrow[3]);
}
echo "
NamePosition
%s %s%s
n";
include("footer.inc");
?>  

Did you see what happened? The contents of the header file are merged into the program, and PHP executes all the code. Notice how $title is defined before including the header.inc header file. Its value can be accessed by code in header.inc. In this way, the title of the web page is changed. Now you can use the header.inc header file in any program. All you have to do is give the $title variable an appropriate value in each main program.
With the addition of header files, HTML, conditional statements, and loop statements, you can use the most concise code to write various complex programs with different functions. Header files are more effective when used together with functions, as we will see later.
Next, we will introduce the exciting part: data verification. >>

Second page Data verification
Imagine this situation: We have designed the database properly, and now we ask the user to enter information to write to the database. Suppose you have a field that requires numeric information, such as price; and a lovely user enters text information in this column, causing a failure in the execution of your application. MySQL database refused to accept the text type data you provided in the SQL statement and made a "stern protest" to you.
What to do? You need to use data validation to prevent the above situation from happening.
To put it simply, data verification means that we check the data (usually passed by the user through an HTML form) to see if it follows certain rules. The rules can be diverse, for example, a certain data element cannot be empty, or the content of a certain data item must meet certain requirements (for example, in the previous example, the requirement must be numbers instead of text, or the requirement in the email address Be sure to include an "@" character, etc.).
Data verification can be done on the server side or on the client side. PHP is used for data verification on the server side, while JavaScript or other client-side scripting languages ​​can provide data verification functions on the client side. This article is about PHP, so we focus on server-side verification here. If you want to find some ready-made data verification programs that run on the client, you can check out the NetMonkey library.
Putting the database aside for now, let’s first talk about PHP’s data verification method. If you are willing (or you want to record the data we want to verify), you can add other fields to the employee database created earlier. It is very simple, just use the MySQL ALTER statement.
There are several PHP functions that can be used for data verification, some are very simple, and some are more complex. Among them, strlen() is a relatively simple function, which can tell us the length of a variable.
A bit more complicated is ereg(), this function can process complete regular expressions to perform complex verification. I don't want to go too deep into regular expressions, as many books are devoted to this subject. But I'll give some simple examples on the next page.
Let’s start with a simple example. The following program checks whether a variable exists.
Copy code The code is as follows:



if ($submit) {
if (!$first || !$last) {
           $error = "Sorry, you must fill in all fields!"; >if (!$submit || $error) {
echo $error;
?>



First column:
Second column:


} // if end
?>



The key part of this program is the nested conditional statement. The first layer checks if the user pressed the button that sent the data. If so, the program then checks whether both $first and $last variables exist. The || symbol means "or" and the ! symbol means "not". That program description in general language is "If $first does not exist or $last does not exist, then set the $error variable to the following value."
Next, we go one step further and check the length of a piece of text. This is necessary to check user passwords, because you don't want some lazy user to enter a password of only one or two characters, and may be asked to enter a six-digit password.
We have already talked about the strlen() function. It simply returns a number equal to the number of characters contained in the variable being measured. Here, I modify the above program and check the lengths of $first and $last.


Copy code

The code is as follows:
if ($submit) {
if (strlen($first) < 6 || strlen($last) < 6) {
$error = "Sorry, you must fill in all fields!" ;
} else {
// Process form input content
echo "Thank you!";
}
}
if (!$submit || $error) {
echo $error;
?>



First column :
Second column:


} // end of if
?>




You can execute this program and enter six words or less. This check is simple but effective. >>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318114.htmlTechArticlePage 1 Basic Functions Welcome to the third and final lesson of this tutorial. If you have studied the first and second lessons, then you have mastered the basics of MySQL and PHP installation and programming...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn