Php/mysql 3rd-The next day (i)

Source: Internet
Author: User
Tags contains mysql php and php and mysql printf
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.

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;? PHP


$db = mysql_connect ("localhost", "root");

mysql_select_db ("MyDB", $db);

$result = mysql_query ("SELECT * FROM Employees", $db);

echo "$#@60;table border=1$#@62;\n";

Echo "$#@60;tr$#@62;$#@60;td$#@62; name $#@60;/td$#@62;$#@60;td$#@62; position $#@60;/td$#@62;$#@60;/tr$#@62;\n";

while ($myrow = Mysql_fetch_row ($result)) {

printf ("$#@60;tr$#@62;$#@60;td$#@62;%s%s$#@60;/td$#@62;$#@ 60;td$#@62;%s$#@60;/td$#@62;$#@60;/tr$#@62;\n ", $myro
W[1], $myrow [2], $myrow [3]);


}

echo "$#@60;/table$#@62;\n";

? $#@62;

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

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

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.

Use the while () loop? The advantage is that if a database query does not return any records, you will not receive an error message. 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.

 

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;? 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 "$#@60;table border=1$#@62;\n";

echo "$#@60;TR$#@62;$#@60;TD$#@62; name $#@60;/td$#@62;$#@60;td$#@62; address $#@60;/td$#@62;$#@60;/tr$#@62;\n";

Do {

printf ("$#@60;tr$#@62;$#@60;td$#@62;%s%s$#@60;/td$#@62;$#@60;td$#@62;%s$#@60;/tr$#@62;\n", $ Myrow["A",
$myrow ["Last"], $myrow ["Address"]);

while ($myrow = Mysql_fetch_array ($result));

Echo "$#@60;/table$#@62;\n";

} else {

echo "Sorry, no record found!" ";

}


$#@62;


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


$#@60;/html$#@62

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, the 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.

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: $#@60;a href= "http://my_machine/ Mypage.php3?id=1 "$#@62; 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.

 

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? 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 ("$#@60;a href=\"%s?id=%s\ "$#@62;%s%s$#@60;/a$#@62;$#@60;br$#@62;\n",
$PATH _info, $myrow ["id"], $myrow ["a"], $myrow ["last"]);

while ($myrow = Mysql_fetch_array ($result));

} else {

echo "Sorry, no record found!" ";

}

? $#@62;

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

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

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:

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? 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:%s\n$#@60;br$#@62;", $myrow ["a"]);

printf ("Surname:%s\n$#@60;br$#@62;", $myrow ["last"]);

printf ("Address:%s\n$#@60;br$#@62;", $myrow ["addresses"]);

printf ("Position:%s\n$#@60;br$#@62;", $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 ("$#@60;a href=\"%s?id=%s\ "$#@62;%s%s$#@60;/a$#@62;$#@60;br$#@62;\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!" ";

}

}

? $#@62;

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

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

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.



Related Article

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.