if ($myrow = mysql_fetch_array($result)) {
do {
printf("
%s %sn",
$PATH_INFO, $myrow["id"] , $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
echo "Sorry, no record found ! ";
}
?>
Nothing special here, just 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 instead of treating them as program code. Also pay attention to the usage of the variable $PATH_INFO. This variable is accessible in all programs and is used to save the name and directory location of the program itself. The reason why we use it is because we need to call the program itself in the page. Using $PATH_INFO, we can ensure that even if the program is moved to other directories or even on other machines, we can ensure that the program is called correctly.
As I just mentioned, the web page generated by the program contains hyperlinks that call the program itself again. However, when called again, some query parameters will be added.
When PHP sees that the query parameter string contains a pair format such as "name=value", it will do some special processing. It will automatically generate a variable with the same name and value as those given in the query parameter string. This function allows us to determine whether the program is executed for the first time or the second time in the program. All we have to do is ask PHP if the $id variable exists.
When I know the answer to this question, I can display some different results the second time I call the program. Please see:
Copy code The code is as follows:
< ;?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( "First name: %sn
", $myrow["first"]);
printf("Last name: %sn
", $myrow["last"]);
printf("Address : %sn
", $myrow["address"]);
printf("Position: %sn
", $myrow["position"]);
} else {
// show employee list
// Show 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, display the list
do {
printf("
% s %sn", $PATH_INFO,
$myrow["id"], $myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
// No records to display
echo "Sorry, no records found! "; Comments are added to explain what is going on. You can use // to add single-line comments, or use /* and */ to enclose large paragraphs of comments.
Here we have learned the first real comment. Useful PHP/MySQL script! Now, we want to see how to add the web table and send data to the database.
Page 4: Sending data to the server
Now we read the data from the database. There is not much difficulty anymore. But how to send data to the database? In fact, this is not a problem with PHP.
First choice, we create a web page with a simple table. >Copy code
The code is as follows:
http://www.bkjia.com/PHPjc/318118.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318118.htmlTechArticleFirst page of while loop In this lesson, we will continue to go deeper and use PHP and MySQL to write Come up with some simple yet useful pages. Let's start with the database we created yesterday, showing the library...