First page 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.
Copy Code code as follows:
<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%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 page If-else
Please see the following procedure.
Copy Code code as follows:
<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%s</td><td>%s</tr>n", $myrow ["a"],
$myrow ["Last"] and $myrow ["Address"]);
while ($myrow = Mysql_fetch_array ($result));
echo "</table>n";
} else {
echo "Sorry, no record found!" ";
}
?>
</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.
Third page First program script
We've just learned the loop statement, and we'll see how to use it in a more practical example. But before that, you should know how to work with Web tables, query parameter strings, and the Get and post methods of a form. Not long ago we just an article to introduce this part of the content, if you are not familiar with this part can look at the article.
Now, we're going to handle the query parameter string, and as you know, there are three ways to write the parameter contents to the query parameter string. The first is to use the Get method in the table; the second is to add the query parameters to the URL in the browser's address bar, and the third is to embed the query parameter string into the hyperlinks of the Web page so that the contents of the hyperlink are as follows: <a href= "http://my_machine/ Mypage.php3?id=1 ">. We're going to use the last one.
First, we'll check our database and list the names of the employees. Look at the following program, most of which we are already familiar with.
Copy Code code as follows:
<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%s</a><br>n",
$PATH _info, $myrow ["id"], $myrow ["a"], $myrow ["last"]);
while ($myrow = Mysql_fetch_array ($result));
} else {
echo "Sorry, no record found!" ";
}
?>
</body>
There's nothing special here, but the printf function is a little different. So let's take a closer look.
The first thing to note is that all quotes are preceded by a backslash. This backslash tells PHP to display the following characters directly, not the following characters as the program code to handle. Also pay attention to the use of variable $path_info. The variable is accessible in the program used to hold the program's own name and directory location. We use it because we want to call the program itself in the page. With $path_info, we can do that even if the program is moved to another directory, or even to other machines, we can ensure that the program is invoked correctly.
As I mentioned earlier, a program generates a Web page that contains hyperlinks that call the program itself again. However, when called again, some query parameters are added.
When PHP sees that the query parameter string contains a pair format such as "name = value", some special processing is done. It automatically generates a variable whose 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 first time the program is executed or the second time. All we have to do is ask Php$id if this variable exists.
When I know the answer to this question, I can show some different results when I invoke the program the second time. Please see:
Copy Code code as follows:
<body>
<?php
$db = mysql_connect ("localhost", "root");
mysql_select_db ("MyDB", $db);
Display individual record
Show single record content
if ($id) {
$result = mysql_query ("SELECT * FROM Employees WHERE id= $id", $db);
$myrow = Mysql_fetch_array ($result);
printf ("Name:%sn<br>", $myrow ["a"]);
printf ("Surname:%sn<br>", $myrow ["last"]);
printf ("Address:%sn<br>", $myrow ["addresses"]);
printf ("Position:%sn<br>", $myrow ["position"]);
} else {
Show Employee List
Show list of employees
$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%s</a><br>n", $PATH _info,
$myrow ["id"], $myrow ["a"], $myrow ["last"]);
while ($myrow = Mysql_fetch_array ($result));
} else {
No records to display
No records to show
echo "Sorry, no record found!" ";
}
}
?>
</body>
The program started getting complicated, so I added a note here to explain what happened. You can add a single-line comment with///////to enclose a large paragraph of comments.
Here, we have learned the first really useful php/mysql script program! Now we're going to look at how to add a Web table and send data to the database.
Page fourth sends data to the server
Now we don't have much difficulty reading data from the database. But how do you send data to the database in turn? In fact, this is not a problem with PHP.
First, we create a Web page with a simple table.
Copy Code code as follows:
<body>
<form method= "POST" action= "<?php echo $PATH _info?>" >
Name: <input type= "text" name= "first" ><br> surname: <input type= "text" Name= "last" ><br> address: <input Type= "text" name= "address" ><br> Position: <input type= "text" name= "position" ><br><input type= " Submit "Name=" value= "input Info" >
</form>
</body>
Current 1/2 page
12 Next read the full text