One, while loop
In this lesson, we'll go further and use PHP and MySQL to write some simple and useful pages. We start with the database we created yesterday and display the data in the library, but we'll touch it a little bit.
First, we use the following code to query the contents of the database.
<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>姓名</td><td>职位</td></tr>\n\";
while ($myrow = mysql_fetch_row($result)) {
printf(\"<tr><td>%s %s</td><td>%s</td></tr>\n\", $myrow[1], $myrow[2], $myrow[3]);
}
echo \"</table>\n\";
?>
</body>
You may have noticed that we have added something new to this program. The most obvious is the while () loop. The loop is to say that as long as the database has records to read (using the Mysql_fetch_row () function), assign the record to the variable $myrow, and then execute the instructions within the braces ({}). Take a closer look here, this part is more important.
We should pay attention to the mysql_fetch_row () function. Here's a little problem, it returns an array, and you must access one of the fields with an array subscript. The first field is labeled 0, the second is 1, and so on. When performing some complex queries, this is simply too cumbersome.
Now let's look more closely at the cycle process. The first few lines of the program we have seen in the example of lesson one. Then, in the while () loop, we read a record from the query result and assign the record to the array $myrow. Next, we use the printf function to display the contents of the data on the screen. Subsequently, the loop executes repeatedly, reading the next record to $myrow. Keep going until all the records have been read.
One advantage of using the while () loop is that you do not receive an error message if the database query does not return any records. When the loop statement is just executed, the loop condition is not satisfied and no data is assigned to the $myrow, and the program runs directly down.
But if the query doesn't return any data, how do we let users know about it? Perhaps we should provide some relevant information to the user. This can be done, and we'll see how to do it below. >>
Second, If-else
Please see the following procedure.
<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>姓名</td><td>住址</td></tr>\n\";
do {
printf(\"<tr><td>%s %s</td><td>%s</tr>\n\", $myrow[\"first\"], $myrow[\"last\"], $myrow[\"address\"]);
}
while ($myrow = mysql_fetch_array($result));
echo \"</table>\n\";
} else {
echo \"对不起,没有找到记录!\";
}
?>
</body>
This program contains a lot of new content, but these are fairly simple. The first is the mysql_fetch_array () function. The function is very similar to mysql_fetch_row (), and is only a bit different: when using this function, we can access the field returned by the field name rather than the array subscript, such as $myrow[\ "first\". So we can save a lot of energy. In addition, the Do/while Loop and if-else conditional judgment statement are added in the program.
The implication of the IF-ELSE conditional decision statement is that if we successfully assign a record to the $myrow variable, we continue; otherwise, we skip to the else part and execute the instructions there.
The Do/while loop is a variant of the while () loop of the user in the previous page. The reason we need to use do/while is that in the original if statement, we have assigned the first record returned by the query to the variable $myrow. If we perform a normal while loop (for example, while ($myrow = Mysql_fetch_row ($result)), we assign the second record to $myrow, and the first record is flushed out. But the Do/while loop allows us to perform a loop-body content once again to determine the cyclic condition. Therefore, we will not accidentally miss the first record.
Finally, if the query results do not have any records, the program executes the statements contained in the else{} section. 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 to other forms, so that there are no records in the query results.
Let's expand the loop If-else code to make the page content richer. I'm sure you'll like it. >>