Linux 版 (精华区)
发信人: netiscpu (网中鸟~~flying), 信区: Linux
标 题: [中英]PHP/MySQL教程(2-5)载入一个数据库
发信站: 紫 丁 香 (Fri Jan 14 01:44:01 2000), 转信
[中英]PHP/MySQL教程(2-5)载入一个数据库
[原作者] Graeme Merrall
[出处] http://hotwired.lycos.com/webmonkey/programming/php/tutorials/tutorial4.html
[译者] netiscpu
PHP/MySQL Tutorial
by Graeme Merrall
======================================================================
载入一个数据库
Load Up a Database
现在我们已经准备好使用MySQL,有一个比较容易的方法,可以得知PHP所能提供的选
项以及你的服务器可以做的事情,那就是使用phpinfo()函数.创建一个如下脚本:
-----------------begin of phpinfo.php3-------------------------------
<html>
<body>
<?php
phpinfo();
?>
</body>
</html>
-----------------end of phpinfo.php3---------------------------------
So now we're ready to plug in MySQL. One handy
way of knowing what options are available in PHP
and what's going on in your server is to use the
phpinfo() function. Create a script with the following:
<html>
<body>
<?php
phpinfo();
?>
</body>
</html>
存盘然后通过Web服务器查看这个脚本的功能,你会发现这是一个非常有用而且有趣
的页面,这些信息告诉你所有关于你的服务器,Web服务器内部环境变量,编译的选项,
等等.在第一部分--扩展部分(Extensions),你应该能找到以MySQL开始的一行,如果
找不到,那就说明由于某种原因无法从PHP中连接MySQL,这时你可以返回前面的安装
步骤,查看一下PHP文档,看看是否遗漏了某些东西.(译者按:以下是phpinfo.php3返
回的部分内容).
-----------------------Part of PHPINFO result begin----------------------
PHP Version 3.0.11(PHP版本3.0.11)
by Rasmus Lerdorf, Andi Gutmans, Zeev Suraski, Stig Bakken, Shane Caraveo,
Jim Winstead, and countless others.
System: Windows 95/98 4.10
Build Date: Jun 26
..........................................................................
Extensions(PHP扩展部分)
..........................................................................
Extensions Additional Information
MySQL(请注意这一行) Allow persistent links: Yes
Persistent links: 0/Unlimited
Total links: 0/Unlimited
Client API version: 3.22.9-beta
Basic Functions No additional information.
PHP_DL Dynamic Library support enabled.
PHP_dir No additional information.
PHP_filestat No additional information.
PHP_file No additional information.
PHP_head No additional information.
Sendmail Internal Sendmail support for Windows 4
Debugger No additional information.
Syslog No additional information.
ODBC/odbc Unified ODBC Support active (compiled
with win32 ODBC)
allow_persistent: 1
max_persistent: -1
max_links: -1
Socket functions No additional information.
Regular
Expressions Bundled regex library enabled
bcmath No additional information.
browscap No additional information.
PHP_pack No additional information.
Win32 COM No additional information.
..........................................................................
Configuration(PHP配置部分)
..........................................................................
php3.ini file path is set to: php3.ini
(后面略去)......
-----------------------Part of PHPINFO result end----------------------
Save and view this script through your Web server.
You'll see a page filled with useful and interesting
information like this. This info tells all about your
server, internal Web server environment variables,
the options that are compiled, and on and on. In
the first section, Extensions, look for a line
beginning with MySQL. If this is missing, then for
some reason MySQL hasn't made it into PHP. Go
back and review the installation steps and check
the PHP documentation to see if you missed
anything.
好了,MySQL已经准备好了,你现在可以进行下一步工作了.
If MySQL is there, then you're ready to roll.
在我们能够从MySQL获取数据之前,要将数据输入进去,此时确实不容易做到,大部分
PHP脚本都带有dump文件,这些文件包含创建和重组一个MySQL数据库.这个处理过程
的输入输出内容超出了本教程的范围,所以我会直接替你做这些工作.
Before we can get data out of MySQL, we have to
put data in it. There's really no easy way to do it at
this stage. Most PHP scripts come with what's
known as a dump file that contains all the data
required to create and populate a MySQL database.
The ins and outs of this process are really outside
the scope of this tutorial, so I'll just do it for you.
MySQL使用它自己的用于权限表,在安装MySQL时,会自动创建一个默认的用户(root),
并且不设口令.再由数据库管理员视情况而定,添加不同的用户,赋予不同的权限,具
体的操作足够再写一篇教程来讲述,这里我们假定只使用root用户,在安装完你自己
的服务器和数据库之后,首要的事情就是为root用户设置一个口令.
MySQL uses its own user permissions table. At
setup, a default user (root) is automatically created
with no password. It's up to the database
administrator to add other users with various
permissions, but I could write a whole other article
on that, so we'll stick with using the root user. If
you set up your own server and database, it's vital
that you assign a password to the root user.
无论如何,我们终于可以开始操作数据库了,对于Win32用户来说,非常抱歉的是,要操
作MySQL数据库,有时候要在DOS窗口下运行命令,或者在"start->run"中输入命令,记
住输入MySQL/bin目录的完整路径名.Unix用户可以在MySQL/bin目录下运行命令,但
是需要以"./"开始,否则可能无法运行命令(译者按:这是因为在Unix中,通常当前路
径不在搜索路径中).
Anyway, let's get on with the database. For Win32
users, I'm sorry, but this requires some DOS work.
You'll have to use a DOS window or type everything
in the Run window. Don't forget to type in the path
to the location of the MySQL/bin directory with your
commands. Unix users can work from the MySQL bin
directory, but you may have to start each command
with ./ so the programs run.
首先,我们要创建一个真实的数据库,在命令行状态下,输入:
mysqladmin -u root create mydb
该命令会创建一个名为"mydb"的数据库,命令中的"-u root"参数告诉MySQL,我们以
root身份进行创建数据库.
The first thing we need to do is create the actual
database. From the command line, type:
mysqladmin -u root create mydb
That creates a database called "mydb." The flag
tells MySQL that we're doing this as the root user.
然后,我们将用一个大家都熟悉的例子说明如何增加数据,这里需要我前面提到的
那个dump文件,如果你对其工作原理感兴趣,可以看看MySQL带的在线文档或者浏览
http://www.turbolift.com/mysql/.
Next we'll add some data using everyone's favorite
example, the employees database. We're going to
need that dump file I mentioned earlier. If you're
interested in how it goes together, review the
manual that comes with MySQL or check out
http://www.turbolift.com/mysql/.
将以下这部分文本拷贝到一个文件中,并且保存为MySQL/bin目录下的mydb.dump:
-----------------------begin of mydb.dump-----------------------------
CREATE TABLE employees (id tinyint(4) DEFAULT '0' NOT NULL
AUTO_INCREMENT, first varchar(20), last varchar(20),
address varchar(255), position varchar(50), PRIMARY KEY (id),
UNIQUE id (id));
INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St,
Cityname','Marketing Manager');
INSERT INTO employees VALUES (2,'John','Roberts','45 There St ,
Townville','Telephonist');
INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd,
Snowston','Doorman');
-----------------------end of mydb.dump-----------------------------
Copy and paste the following text to a file and
save it in MySQL's bin directory. (I'll call the file
mydb.dump.)
CREATE TABLE employees ( id tinyint(4) DEFAULT '0' NOT NULL
AUTO_INCREMENT, first varchar(20), last varchar(20),
address varchar(255), position varchar(50), PRIMARY KEY (id),
UNIQUE id (id));
INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St,
Cityname','Marketing Manager');
INSERT INTO employees VALUES (2,'John','Roberts','45 There St ,
Townville','Telephonist');
INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd,
Snowston','Doorman');
如果行数限制,应该保证每个插入声明另起一行,现在我们将其插入到mydb数据库中,
在命令行状态下输入命令:
mysql -u root mydb < mydb.dump
If the lines wrap, make sure that each insert
statement is on a new line. Now we'll insert it into
the mydb database. From the command line, type:
mysql -u root mydb < mydb.dump
执行这个命令时,你应该得不到任何错误信息,如果有出错提示,查看一下mydb.dump
的每一条命令是否正确无误.
You shouldn't get any errors doing this. If you do,
check for incorrect line wrapping.
[未完待续]
--
Enjoy Linux!
-----It's FREE!-----
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: uststf1-nc2.ust.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:213.883毫秒