Page 1 while LOOP
In this lesson, we will continue to use PHP and MySQL to write some simple and useful pages. We started to display the data in the database from the database we created yesterday, but it will be slightly improved.
First, we use the following code to query the database content.
Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("mydb", $ db );
$ Result = mysql_query ("SELECT * FROM employees", $ db );
Echo "<table border = 1> n ";
Echo "<tr> <td> name </td> <td> position </td> </tr> n ";
While ($ myrow = mysql_fetch_row ($ result )){
Printf ("<tr> <td> % s </td> </tr> n", $ myrow [1], $ myrow [2], $ myrow [3]);
}
Echo "</table> n ";
?>
</Body>
</Html>
You may have noticed that we have added some new things in this program. The most obvious is the while () loop. This loop means that as long as there are records in the database that are readable (using the mysql_fetch_row () function), the record is assigned to the variable $ myrow and then braces ({}) are executed ({}). Taking a closer look, this part is important.
Note the mysql_fetch_row () function. There is a small problem here. It returns an array and must access a field in it with an array subscript. The first field subscript is 0, the second is 1, and so on. It is too cumbersome to execute some complex queries.
Now let's take a closer look at the cyclical process. We have seen the first few lines of the program in the example of Lesson 1. Then, in the while () loop, we read a record from the query results and assigned the record to the array $ myrow. Then, we use the printf function to display the data content on the screen. Then, the loop is executed repeatedly, and the next record is read and assigned to $ myrow. This continues until all records have been read.
One advantage of using the while () loop is that if the database query does not return any records, you will not receive the error message. When the loop statement is just executed, the loop condition is not met and no data is assigned to $ myrow. Then the program runs directly.
But if no data is returned for the query, how can we let the user know this? Maybe we should provide some related messages to users. This can be done. Let's take a look at how to do it.>
Page 2 if-else
See the following program.
Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("mydb", $ db );
$ Result = mysql_query ("SELECT * FROM employees", $ db );
If ($ myrow = mysql_fetch_array ($ result )){
Echo "<table border = 1> n ";
Echo "<tr> <td> name </td> <td> address </td> </tr> n ";
Do {
Printf ("<tr> <td> % s </td> <td> % s </tr> n", $ myrow ["first"],
$ Myrow ["last"], $ myrow ["address"]);
} While ($ myrow = mysql_fetch_array ($ result ));
Echo "</table> n ";
} Else {
Echo "sorry, no record found! ";
}
?>
</Body>
</Html>
This program contains a lot of new content, but the content is quite simple. The first is the mysql_fetch_array () function. This function is very similar to mysql_fetch_row () and has only one difference: when using this function, we can use the field name instead of the array subscript to access the fields it returns, such as $ myrow ["first"]. In this way, we can save a lot of effort. In addition, the do/while loop and if-else condition judgment statements are added to the program.
The if-else condition determination statement means that if we successfully assign a record to the $ myrow variable, we will continue; otherwise, we will jump to the else part and execute the commands there.
The do/while loop is a variant of the user's while () loop on the previous page. The reason we need to use do/while is: in the initial if statement, we have assigned the first record returned by the query to the variable $ myrow. If we execute a general while loop (for example, while ($ myrow = mysql_fetch_row ($ result), we will assign the second record to $ myrow, the first record is washed out. However, the do/while LOOP allows us to determine the loop condition after executing the content of the loop body. Therefore, we will not accidentally miss the first record.
Finally, if the query results do not have any records, the program will execute the statements contained in else. If you want to see the execution of this part of the program, you can change the SQL statement to SELECT * FROM employees WHERE id = 6, or change it to another form, so that there is no record in the query result.
Next we will expand the circular if-else code to enrich the page content. I believe you will like it.
Page 3 first program script
We have just learned a circular statement. Next we will look at how to use it in a more practical example. However, before that, you should know how to process Web tables, query parameter strings, and GET and POST methods of forms. We have just introduced this part of the article. If you are not familiar with this part, you can refer to it.
Now, we need to process the query parameter string. As you know, there are three methods to write the parameter content into the query parameter string. The first is to use the GET method in the table; the second is to add query parameters directly when entering the URL in the address bar of the browser; the third is to embed the query parameter string into the hyperlink of the webpage, make the hyperlink content as follows: <a href = "http: // my_machine/mypage. php3? Id = 1 ">. Now we need to use the last method.
At the beginning, we will query our database to list employee names. Take a look at the following program. We are familiar with most of the content.
Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("mydb", $ db );
$ Result = mysql_query ("SELECT * FROM employees", $ db );
If ($ myrow = mysql_fetch_array ($ result )){
Do {
Printf ("<a href =" % s? Id = % s "> % s </a> <br> n ",
$ PATH_INFO, $ myrow ["id"], $ myrow ["first"], $ myrow ["last"]);
} While ($ myrow = mysql_fetch_array ($ result ));
} Else {
Echo "sorry, no record found! ";
}
?>
</Body>
</Html>
There is nothing special here, but the printf function is somewhat different. Let's take a closer look.
Note that all quotation marks are preceded by a backslash. This backslash tells PHP to directly display the following characters, instead of processing the subsequent characters as program code. Pay attention to the usage of the variable $ PATH_INFO. This variable can be accessed in the program in use and is used to save the name and directory location of the program. We use it because we need to call the program itself on the page. With $ PATH_INFO, we can ensure that this program can be correctly called even when the program is moved to another directory or even on another machine.
As I mentioned earlier, the web page generated by the program contains hyperlinks that call the program itself again. However, some query parameters will be added when you call it again.
When PHP sees that the query parameter string contains a pair format such as "name = value", it will perform some special processing. It automatically generates a variable. Both the variable name and value are the same as the name and value given in the query parameter string. This function allows us to determine in the program whether the program is executed for the first time or for the second time. All we have to do is ask whether the variable PHP $ id exists.
When I know the answer to this question, I can display different results when calling the program for the second time. See:
Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("mydb", $ db );
// Display individual record
// Display the content of a single record
If ($ id ){
$ Result = mysql_query ("SELECT * FROM employees WHERE id = $ id", $ db );
$ Myrow = mysql_fetch_array ($ result );
Printf ("Name: % sn <br>", $ myrow ["first"]);
Printf ("last Name: % sn <br>", $ myrow ["last"]);
Printf ("address: % sn <br>", $ myrow ["address"]);
Printf ("position: % sn <br>", $ myrow ["position"]);
} Else {
// Show employee list
// Display the employee list
$ Result = mysql_query ("SELECT * FROM employees", $ db );
If ($ myrow = mysql_fetch_array ($ result )){
// Display list if there are records to display
// If there are records, the list is displayed
Do {
Printf ("<a href =" % s? Id = % s "> % s </a> <br> n", $ PATH_INFO,
$ Myrow ["id"], $ myrow ["first"], $ myrow ["last"]);
} While ($ myrow = mysql_fetch_array ($ result ));
} Else {
// No records to display
// No record is displayed
Echo "sorry, no record found! ";
}
}
?>
</Body>
</Html>
The program began to become complicated, so I added a comment here to explain what happened. You can use // to add a single line comment, or use/* and */to enclose the comment of a large segment.
Here, we have learned the first truly useful PHP/MySQL script program! Now, let's look at how to add a Web table and send data to the database.
Page 4: send data to the server
It is no longer difficult to read data from the database. But how can we send data to the database in turn? In fact, this is not a problem with PHP.
First, create a webpage with a simple table.
Copy codeThe Code is as follows:
<Html>
<Body>
<Form method = "post" action = "<? Php echo $ PATH_INFO?> ">
Name: <input type = "Text" name = "first"> <br> last name: <input type = "Text" name = "last"> <br> address: <input type = "Text" name = "address"> <br> position: <input type = "Text" name = "position"> <br> <input type = "Submit" name = "submit" value = "input">
</Form>
</Body>
</Html>