Linux 版 (精华区)

发信人: netiscpu (网中鸟~~flying), 信区: Linux
标  题: [中英]PHP/MySQL教程(3-1)使用循环语句
发信站: 紫 丁 香 (Mon Jan 17 03:41:51 2000) WWW-POST

[中英]PHP/MySQL教程(3-1)使用循环语句
 
[原作者] Graeme Merrall
[出处] http://hotwired.lycos.com/webmonkey/programming/php/tutorials/tutorial4
.html
[译者] netiscpu

PHP/MySQL Tutorial
by Graeme Merrall 

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

3-1 使用循环语句

3-1 Getting Loopy 

在这一节课程中,我们将学习如何使用PHP/MySQL来创建一些简单而有用的页面,
让我们从现实上一节课创建的数据库开始,不过这回我们要把显示界面做得美观
一些.

    In this lesson, we're going to dive right in and create some 
    simple yet useful pages using PHP and MySQL. Let's start by 
    displaying the database we created yesterday, but with a little 
    more panache. 

首先用先面这段代码查询数据库:

-------------------------begin of query.php3-----------------------

    <html>

    <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</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>

    </html>

-------------------------end of query.php3-----------------------

    First, let's query our database using the following code. 


    <html>

    <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</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>

    </html>

你大概会注意到,我们引入了一些新特性,最显而易见的就是while()循环,

    You probably noticed that we introduced a couple of new 
    features here. Most obvious is the while() loop. The loop 
    says that as long as there are new rows of data to be grabbed 
    (using the mysql_fetch_row() function), assign that row to 
    the $myrow variable. Then execute the instructions between 
    the curly brackets ({}). Take a look for a second, and this 
    should make sense. 

再来看看函数"mysql_fetch_row()",使用"mysql_fetch_row()"有个小问题,它只
返回一个数组,故只能支持使用数字来引用各个单独字段,所以"0"代表数据库中
的第一个字段,"1"代表第二个字段,..以此类推.如果要进行复杂的查询,使用这
种方式将无疑是一个恶梦.

    The mysql_fetch_row() function bears a closer look. One small 
    problem with mysql_fetch_row() is that it returns an array that 
    supports only numeric references to the individual fields. So the
    first field is referred to as 0, the second as 1, and so on. With 
    complex queries this can become something of a nightmare. 

现在让我们更深入地看看循环语句,你会发现前面几行已经在第一节课程的例子
中出现过,接着,在while()循环中,我们将从数据中获取的每一行结果保存到数组
$myrow中,然后,我们将数组$myrow所包含的内容输出到屏幕上.做完之后,再重复
一遍,另外数据库中的另一行结果就被赋予数组$myrow,直到取完所有的行为止.

    Now let's examine the loop in more detail. The first few lines 
    you'll recognize from the example in Lesson 1. Then in the while() 
    loop we fetch a row from the result and assign it to the array
    $myrow. Then we print the contents of the array on the screen with 
    the printf function. After that it loops around again, and another 
    row is assigned to $myrow. It will do this until it runs
    out of rows to grab. 

while()循环好有一个好处,如果你的查询没有返回纪录,你将不会得到错误信息,
由于第一次运行的时候,没有任何数据赋值给变量$myrow,程序将会直接运行下去.

    The great thing about a while() loop is that if your query returns 
    no records, you won't get an error message. The first time through 
    there won't be any data to assign to $myrow, and the program will 
    just move on. 

但是如果一个查询没有返回数据,我们也无法让用户知道这一情况,不过我们也许
该提供一些信息给用户.这是完全可以做到的,具体的方法将在下一节讨论.

    But if the query returns no data, we have no way of letting the 
    user know, and we should probably provide some sort of message. 
    This is possible, so let's do it. 
 
[未完待续]
--

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

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