Linux 版 (精华区)
发信人: netiscpu (网中鸟~~flying), 信区: Linux
标 题: [中英]PHP/MySQL教程(2-6)深入一步
发信站: 紫 丁 香 (Fri Jan 14 01:49:40 2000), 转信
[中英]PHP/MySQL教程(2-6)深入一步
[原作者] Graeme Merrall
[出处] http://hotwired.lycos.com/webmonkey/programming/php/tutorials/tutorial4.html
[译者] netiscpu
PHP/MySQL Tutorial
by Graeme Merrall
======================================================================
深入一步
Pull It Back Out
好了,现在我们已经得到数据库的数据了,下面就可以应用这些数据做一些试验.将下
面这段代码粘贴到一个文本文件,并且保存成Web服务器文档树下以".php3"为扩展名
的文件.
--------------------------Cut from here----------------------------------
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"first"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address: %s<br>\n", mysql_result($result,0,"address"));
printf("Position: %s<br>\n", mysql_result($result,0,"position"));
?>
</body>
</html>
--------------------------Cut end----------------------------------------
OK, now we've got our data in the database. Let's do something
with it. Copy and paste the following into a text file and save
it in your Web server document tree with a .php3 extension.
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"first"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address: %s<br>\n", mysql_result($result,0,"address"));
printf("Position: %s<br>\n", mysql_result($result,0,"position"));
?>
</body>
</html>
让我们来看看这段代码的含义,"mysql_connect()"函数用来以某个特定用户的身
份来打开一个指定主机上的MySQL服务器,本例中以超级用户"root"打开本地主机
"localhost"的上的MySQL服务器中的数据库"mydb",如果需要的话,你还要在该函
数中指定用户访问口令,数据库连接结果存放在变量"$db"中.
Let's explain what happens here. The mysql_connect() function
opens a link to a MySQL server on the specified host (in this
case it's localhost) along with a username (root). If you needed
to specify a password, you'd add it here as well. The result of
the connection is stored in the variable $db.
然后,函数"mysql_select_db()"告诉PHP,我们的任何查询都基于数据库"mydb",
我们可以对不同服务器上的数据库建立多个连接,不过现在我们暂时不讨论这个
问题.
mysql_select_db() then tells PHP that any queries we make are
against the mydb database. We could create multiple connections
to databases on different servers. But for now, let's leave it
to this.
下一步,函数"mysql_query()"完成所有重要的工作,根据数据库连接标识($db),
它向MySQL服务器发送一条SQL语句,MySQL处理之后,返回结果保存在$result变
量中.
Next, mysql_query() does all the hard work. Using the database
connection identifier, it sends a line of SQL to the MySQL server
to be processed. The results that are returned are stored in the
variable $result.
最后,函数"mysql_result()"用来显示查询结果的各个域的值,根据$result,
我们先得到第一列,用数字"0"代表,同时显示指定域的值.
Finally, mysql_result() is used to display the values of fields
from our query. Using $result, we go to the first row, which is
numbered 0, and display the value of the specified fields.
函数"printf"的语法看起来有一点点奇怪,如果你没有用过Perl或者C编程过的
话.在上面的每一个printf函数中,"%s"代表该表达式后半部分的变量值,比如,
本例中代表mysql_result($result,0,"position")所返回的值,在实际运行时,
你可以将它视为一个字符串,有关printf函数更多的知识,请查看PHP在线文档.
The syntax of the printf function may seem a little odd if you
haven't used Perl or C before. In each of the lines above, %s
indicates that the variable in the second half of the expression
(e.g., mysql_result($result,0,"position")) should be treated as
a string and printed. For more on printf, see the PHP documentation.
回顾一下我们所做的,在本课中,我们成功地编译,安装和配置了MySQL/PHP,同时,
我们还执行了一个简单的PHP脚本,以获取某些信息.在第2课中,我们还将进行更
输入的学习,你将学会如何显示多条数据库纪录以及如何往数据库添加数据,从
数据库中读取数据.
So there we have it. We successfully complied, installed, and
configured MySQL and PHP, and we've executed a simple script
to retrieve some information. In Lesson 2, we'll do some clever
stuff to display multiple records and even send data to and from
the database.
继续跟我来!
Come on back, now.
[未完待续]
--
Enjoy Linux!
-----It's FREE!-----
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: uststf1-nc2.ust.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.337毫秒