本文介绍堪称Web数据库黄金组合的PHP/MySQL如何构筑一个网络数据库应用的方法,PHP是一个类似微软ASP的服务器端的嵌入式超文本处理语言,是建立动态网站的强大工具。而MySQL是一个轻型SQL数据库服务器,可运行在多种平台上,包括Windows NT和Linux,它还有一个GPL版本,MySQL被认为是建立数据库驱动的动态网站的最佳产品。PHP、MySQL和Apache是Linux平台网站的最佳拍档。该教程是一个3日课程,从安装到应用都有详细描述。
一、PHP/MySQL简介
您应该听说过开放源代码软件(OSS),除非最近六到八个月来您一直住在火星上。这场运动有着巨大的冲击力,已引起一些大公司的注意。象Oralce、Informix,还有多家公司都开始把他们的主力数据库产品移植到OSS的产物之一 - Linux操作系统上。
如果您有足够的技术力量,拥有一个复杂庞大的关系数据库系统(RDBMS)则是如虎添翼。但是也许您对数据库还刚刚入门,您刚刚读了Jay的文章,决定自己也来搞一个数据驱动的网站。可是您也许会发现您缺乏必要的资源来运行ASP服务器或昂贵的数据库系统(您也不需要这些东西)。您需要一些免费的、支持Unix的东西。
那么我建议您使用PHP和MySQL。这两样东西加在一起,对于开发数据驱动的网站这项工作而言,真是最佳组合。其实用不着我多费唇舌解释。一项由Netcraft组织的非官方调查显示,应用PHP的主机数目由1998年6月的7,500台跃升至1999年3月的410,000台。不错吧?这两种软件的组合还在Webcon98大会上赢得了年度数据库产品大奖,还得了一座漂亮的奖杯。
MySQL是一个小巧灵珑的数据库服务器软件,对于小型(当然也不一定很小)应用系统是非常理想的。除了支持标准的ANSI SQL语句,它还支持多种平台,而在Unix系统上该软件支持多线程运行方式,从而能获得相当好的性能。对于不使用Unix的用户,它可以在Windows NT系统上以系统服务方式运行,或者在Windows 95/98系统上以普通进程方式运行。
PHP是一种服务器端解释的脚本语言。如果你接触过ASP的话,那么您对于在HTML页面中嵌入代码应该是比较熟悉了。PHP代码在服务器一端被解释转变成普通的HTML页面内容,送给浏览器一端。这种模式使得我们可以用它来完成相当复杂的功能。
除了免费这一点(当然,MySQL也有一些使用许可方面的限制),PHP-MySQL的组合还可以跨平台运行,这意味着您可以在Windows上开发,然后在Unix平台上运行 硗猓琍HP也能作为标准的CGI进程来运行,此时它是一个独立的脚本解释器,或者是Apache的一个嵌入模块。
如果您有兴趣使用其他数据库服务器的话,PHP也支持Informix、Oracle、Sybase、Solid和PostgreSQL ,以及通用的ODBC。
PHP支持Internet开发的一些前沿技术。这些技术包括身份认证、XML、动态图象生成、WDDX、共享内存,以及动态PDF文档等等,不一而足。如果您还不满意的话,PHP是很容易扩展的,所以只要您有编程能力,您尽可以自己大展身手一番。
最后要说的是,两种软件都是大量程序员协同开发的,因此文档及邮件列表等支持方式很多。程序错误的修正很快,而如果您要求加入新功能的话,总会有人考虑您的要求,并且在可行性足够高的前提下加以实现。
说得够多了!我们来看看这篇教程里都有些什么内容吧。
第一课讲的是在Unix和Windows环境下安装这两个软件。如果您不太关心这个问题(也许您是在ISP的服务器上开发),您可以直接跳到第一个示例程序,从那里开始您的奇妙之旅。
在第二课里,我们要学习一些更加复杂的脚本功能,比如循环、处理用户输入,以及与数据库交换数据等等。
第三课要讲的是确认功能,以及如何使您的脚本程序清晰简练。
我们开始吧
二、安装MySQL
快点行动,拿到这些软件包,仔细研究一下吧。这可不是件容易的事儿。你从获取软件包、编译软件包到安装软件包的过程中都有不少的选择项。我们先从MySQL开始,因为有了它才能运行PHP。
MySQL中央网站是http://www.mysql.com/。为了方便人们下载使用(这软件还是比较大的),全球有许许多多的镜像网站。为更有效地利用因特网,请找一个就近的网站下载。
You will also have multiple choices at this time. If you are an enthusiast, you can download the source code; otherwise, there are precompiled binaries online that run on various platforms and can be downloaded directly.
In addition, MySQL has also prepared a shareware version for Windows users, which has a slightly lower version number. If you want the latest version, you have to pay for a software license. MySQL also provides an ODBC driver that enables front-end applications to access the MySQL database. Some other details are available on its website, you can check it out.
The pre-compiled Unix version and Windows version are very simple, just unpack and use it, no need to explain too much. Therefore, let's introduce the compilation of source code. Windows users, please remember that you need to run the mysqld program, which is located in the mysql/bin directory.
Download the compressed software package file and place it in a directory. Use gzip and tar to decompress and unpack. A faster way is to use the following command:
gunzip $#@60; mysql-xxxx.tar.gz | tar xvf -
Among them, xxxx is a version tag you choose. The above command will create a directory named mysql-xxxx, and all source program files are in this directory. Change to the directory by executing the cd mysql-xxxx command and carefully read the multiple README files and INSTALL files in it. These files may be of great help when encountering certain problems.
MySQL comes with some convenient configuration scripts. You can let these programs do a lot of the work for you by simply typing ./configure. If you need to explicitly specify what to do, you can use the ./configure -help command, which will give you a list of options you can use. For example, if you are compiling on a machine with less memory, you can use the --with-low-memory option. I like to install MySQL in one total directory rather than in multiple directories on the machine, so I will specify the installation directory and specify the -prefix option.
You can also specify many other options, such as which parts to omit during compilation, which parts to keep, etc. We might as well assume that they are all installed in the server's /usr/local/mysql directory. This means we will type the ./configure -prefix=/usr/local/mysql command.
When the configuration script runs, it will check your system and then generate some files to ensure smooth compilation. If the script fails, it will also display some helpful error messages telling you why it failed. We often encounter scripts that fail when looking for multi-threading library files. At this time, you should check whether MIT-pthreads has been installed in the system. If not, please install the software. Linux users must install LinuxThreads. These library files are very critical for MySQL's multi-threaded running mode (that is, running multiple versions of itself).
If everything goes smoothly, just type the make command and go make a cup of coffee. MySQL is a fairly complex program and takes a while to compile. If you find errors, you can check the documentation to see if you missed any preparations related to your operating system.
Next, type the make install command, and all necessary files will be installed to the corresponding directory. You're almost ready to get started! If you are new to MySQL and have never installed MySQL before, you will need to create default permissions to install it, so type...scripts/mysql_install_db to set it up accordingly.
That’s it. Our preparations are complete. All that needs to be done next is to automatically start the database when the machine starts, and automatically close the database when it is shut down. Yes, there is a dedicated script for this job too. The mysql.server start command can start the database, and mysql.server stop can shut down the database. It couldn't be simpler. If you want to start the database manually (so you don't have to restart the machine), you can go to the top-level directory where MySQL is installed (/usr/local/mysql) and type the bin/safe_mysqld & command.
We are already halfway done. Next is the PHP part.
3. Install PHP
OK, when you see this, I hope you have MySQL installed and running. That’s really interesting! Here's PHP... The process is a little easier, but the sheer number of options can still be overwhelming. Of course, there is no need to panic. You can always start over and recompile PHP, adding or removing options as needed.
The home of PHP is http://www.php.net/. This site is extremely informative, ranging from development project content to software bug reports. Like MySQL, you can choose a mirror website nearest you to download. Obviously, you have to go to the Downloads section of the website to download PHP.
You don’t have many choices here. There are some pre-compiled binaries, but those are experimental. If you are not on a Windows platform, download the source code and compile it yourself.
But let’s talk about Windows first. When using PHP, the general approach is to develop on a Windows system and then run it on a Unix server. You may end up choosing this method, which requires you to be familiar with installation on both platforms.
After downloading the Windows binary file, you can use any ZIP decompression program you like to extract the files in the software package to the php3 directory on the C drive. The README file in the software package partially explains the details of the installation process, but here we will make a reader's digest of the key contents of this file: If you do not want to install PHP in the C:php3 directory, but elsewhere. directory, then you have to edit the .inf file in the extracted file.
In the php3 directory, you will find many .dll files. Move all .dll files whose file names do not start with php_ to the windowssystem directory. Then, rename the php.ini-dist file to php3.ini and move it to the windows directory. If you open this file, you'll see that there are a lot of interesting things you can change. Now