Linux 版 (精华区)

发信人: netiscpu (网中鸟~~flying), 信区: Linux
标  题: [中英]PHP/MySQL教程(3-2)Stay Informed
发信站: 紫 丁 香 (Mon Jan 17 04:12:44 2000) WWW-POST

[中英]PHP/MySQL教程(3-2)Stay Informed
 
[原作者] Graeme Merrall
[出处] http://hotwired.lycos.com/webmonkey/programming/php/tutorials/tutorial4
.html
[译者] netiscpu

PHP/MySQL Tutorial
by Graeme Merrall 

======================================================================


3-2 Stay Informed (不知道怎么翻译,保留原标题吧;)


请看下面这个程序:

-------------------------begin of exam3-2a.php3----------------------

     <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>Position</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 "Sorry, no records were found!";   

     }

     ?>

     </body>

     </html>

-------------------------end of exam3-2a.php3----------------------
     
     Take a look at this script. 

     <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>Position</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 "Sorry, no records were found!";   

     }

     ?>

     </body>

     </html>

这个程序又引进了一些新特性,不过都很简单.首先是函数"mysql_fetch_array()",
它完全能实现"mysql_fetch_row()"的功能,不过多了一个非常好用的功能:我们
可以用它来用字段的名字来引用字段的内容(比如$myrow["first"]),而不是用数
字来引用,这帮我们解决了很多头疼的问题.前面我们已经介绍了do/while循环语
句和if-else条件判断语句的用法.

      There are a number of new features introduced
      here, but they're quite simple. First, there's the
      mysql_fetch_array() function. This is exactly the
      same as mysql_fetch_row() with one nice
      exception: Using this function, we can refer to
      fields by their names (such as $myrow["first"])
      rather than their numbers. This should save us
      some headaches. We've also introduced a do/while
      loop and an if-else statement. 

使用if-else语句,当我们可以将表格的某一行赋值给$myrow时,继续执行;否则,可以
跳转到相应的其他部分,再执行代码.

      The if-else statement says that if we can assign a
      row to $myrow, then continue; otherwise skip to
      the else section and do what's in there. 

do/while循环是while()循环的一个变种,我们在上一小节中用到过.这里我们有一个
很好的理由要使用do/while循环:在if语句的初始化过程中,我们将返回的查询结果的
第一行数据赋值给变量$myrow,这时我们执行的是的一般while语句的功能(就像,
while ($myrow = mysql_fetch_row($result)),如果第一行纪录无法存储到变量中,
我们就无法执行do/while内部的语句.do/while循环语句让我们可以测试条件,当那段
代码已经运行过之后.因此,这将不会因为其它的原因而忽略一行数据.

      The do/while loop is a variation of the while() loop
      we used on the last page. We need the do/while
      loop here for a very good reason: With the initial if
      statement, we assigned the first row returned by
      the query to the variable $myrow. If at this point
      we executed a regular while statement (such as
      while ($myrow = mysql_fetch_row($result)), we'd
      be kicking the first record out of the variable and
      replacing it with the second record. But the
      do/while loop lets us test the condition after the
      code has been run once. So there's no chance of us
      accidentally skipping a row. 
      

最后,如果没有返回任何纪录,包含在else{}中的那部分代码将会执行,要想看到这部分
代码的执行动作,只要将SQL条件改成"SELECT * FROM employees WHERE id=6"或者其
它不会返回任何数据的SQL语句就行了.

      Finally, if there are no records returned at all, the
      statements contained in the else{} portion will be
      executed. To see this portion in action, change the
      SQL statement to SELECT * FROM employees
      WHERE id=6 or something else that will return no
      records. 

现在,如果你开始喜欢上它的话,让我们再来对循环语句和if-else判断语句代码做
一些扩展,以生成一个更特别的页面.

      Now let's extend this looping and if-else code to
      make one fancy-schmancy page. You're going to
      love it. 


     next page? 
     
[未完待续]
--

                              Enjoy Linux!
                          -----It's FREE!-----

※ 来源:·紫 丁 香 bbs.hit.edu.cn·[FROM: ustsu54-nc2.ust] 
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.431毫秒