vent you from developing with PHP and MySQL. Both products run on Windows
95/98 and Windows NT/2000/XP/CE.
But in the real world, almost all PHP/MySQL applications are running off some
version of Unix, whether Linux, BSD, Irix, Solaris, HP-UX, or one of the other flavors.
The major advantage of Unix is its inherent stability. Boxes loaded with Linux
have been known to run for months or years without crashing. Linux and BSD also
have the advantage of being free of charge and able to run on standard PC hardware.
If you have any old 486, you can load it up with Linux, MySQL, PHP, and Apache
and have yourself a well-outfitted Web server. You probably wouldnâ€™t want to put it
on the Web, where a moderate amount of traffic might overwhelm it, but it can
serve nicely as a development server, a place where you can test your applications.
THE WEB SERVER
The Web server has what seems to be a fairly straightforward job. It sits there, run-
ning on top of your operating system, listening for requests that somebody on the
Web might make, responding to those requests, and serving out the appropriate Web
pages. In reality, it is a bit more complicated than that, and because of the 24/7
nature of the Web, the stability of the Web server is a major issue.
There are many Web servers out there, but two dominate the market. These are
Apache and Microsoftâ€™s Internet Information Server (IIS).
INTERNET INFORMATION SERVER IIS is deeply tied to the Windows environment
and is a key component of Microsoftâ€™s Active Server Pages (ASP). If youâ€™ve chosen
to go the Microsoft way, youâ€™ll almost certainly end up using IIS.
There is a certain amount of integration between the programming language and
the Web server. At this point, PHP 4 and 5 integrate well with IIS.
APACHE The Apache Web server is the most popular Web server there is. It, like
Linux, PHP, and MySQL, is an open-source project. Not surprisingly, Apache works
best in Unix environments, but also runs just fine under Windows.
Apache makes use of third-party modules. Because it is open source, anyone
with the skill can write code that extends the functionality of Apache. PHP most
often runs as an Apache extension, known as an Apache module.
Apache is a great Web server. It is extremely quick and amazingly stable. The
most frequently stated complaint about Apache is that, like many pieces of Unix
software, the graphical tools for manipulating the application are limited. You alter
Apache by specifying options on the command line or by altering text files. When
you come to Apache for the first time, all this can be a bit opaque.
Though Apache works best on Unix systems, versions also run on Windows
operating systems. Nobody, not even the Apache developers, recommends that
Apache be run on a busy server under Windows. If you have decided to use the
Windows platform for serving Web pages, youâ€™re better off using IIS.
But there are conditions under which youâ€™ll be glad Apache does run under
Windows. You can run Apache, PHP, and MySQL on a Windows 98 machine and
then transfer those applications to Linux with practically no changes to the scripts.
This approach is the easiest way to go if you need to develop locally on Windows
but to serve off a Unix/Apache server.
PHP belongs to a class of languages known as middleware. These languages work
closely with the Web server to interpret the requests made from the World Wide
Web, process these requests, interact with other programs on the server to fulfill the
requests, and then indicate to the Web server exactly what to serve to the clientâ€™s
The middleware is where youâ€™ll be doing the vast majority of your work. With a
little luck you can have your Web server up and running without a whole lot of
effort. And once it is up and running, you wonâ€™t need to fool with it a whole lot.
But as you are developing your applications, you spend a lot of time writing code
that makes your applications work. In addition to PHP, several languages perform
similar functions. Some of the more popular choices are ASP, Perl, and ColdFusion.
Relational database management systems (RDBMSes) provide a great way to store
and access complex information. They have been around for quite a while. In fact,
they predate the Web, Linux, and Windows, so it should be no surprise that there
are many RDBMSes to choose from. All the major databases make use of the
Structured Query Language (SQL).
Some of the more popular commercial RDBMSes are Oracle, Sybase, Informix,
Microsoftâ€™s SQL Server, and IBMâ€™s DB2. In addition to MySQL, there are now two
major open-source relational databases. Postgres has been the major alternative to
MySQL in the open-source arena for some time. For a while, Borland released its
Interbase product under an open-source license and allowed free download and
use. The results of that release are a software project called Firebird.
Why These Products?
Given the number of choices out there, you might be asking yourself why you should
choose PHP and/or MySQL. We answer this question in the following sections.
Programming languages are a lot like shoes. Some look good to some people yet
look really ugly to others. To carry the analogy a little further, some shoes just fit
well on some feet.
What we mean is this: When it comes to Web programming, all languages do
pretty much the same things. They all interact with relational databases, they all
work with file systems, and they all interact with Web servers. The question of
which language is best is rarely a matter of a languageâ€™s ability or inability to per-
form certain actions. Itâ€™s usually more a matter of how quickly and easily you can
do what you need to do.
ITâ€™S FAST AND EASY
What about speed? There are really only three things that we know for sure when it
comes to comparing the speeds of Web-programming languages.
â—† Applications written in C will be the fastest.
â—† Programming in C is rather difficult and takes much longer than program-
ming in any of the other languages mentioned so far.
â—† Comparisons among languages are extremely difficult.
From everything we know, we feel safe in saying that PHP is as fast as anything out
More often than not, choosing a language comes back to the same issues involved
in buying shoes. You want to go with whatâ€™s most comfortable. If youâ€™re like us,
you find that PHP represents the perfect combination of power, structure, and ease
of use. Again, this is largely a matter of opinion, but we do believe that the syntax
of PHP is superior to those of ASP and JSP. And we believe it puts more power at
your fingertips more quickly than ColdFusion and is not as difficult to learn as Perl.
In the end, we believe PHP offers you the best opportunity to develop powerful
Web applications quickly. That generalization made, we do believe there are other
excellent reasons for choosing PHP.
In the rundown of Web architecture, we mentioned that PHP runs on Windows
2000/NT/CE/XP and Unix and with both IIS and Apache. But the cross-platform
abilities of PHP go far beyond these platforms. If you happen to be using Netscape,
Roxen, or just about anything else, it is likely PHP works with it.
Yes, ASP can be run on Linux, ColdFusion can work on Solaris and Linux, and
JSP is adaptable across many platforms. At this point, PHP works as well on as
wide a variety of systems as any other available product.
IT ACCESSES EVERYTHING
What do you need to access in the course of creating your Web applications?
LDAP? IMAP mail server? Oracle? Informix? DB2? Or maybe you need an XML
parser or WDDX functions.
Whatever you need to use, it is more than likely that PHP has a built-in set of
functions that make getting whatever you need very easy. But what if it doesnâ€™t
have something built in that youâ€™d like? That brings us to our next point.
ITâ€™S CONSTANTLY BEING IMPROVED
If you are new to open-source development, you might be surprised by the high
quality of the software. There are thousands of very technical, very talented pro-
grammers out there who love to spend their time creating great, and mostly free,
software. In an active project such as PHP, a variety of developers look to improve
the product almost daily.
It is truly remarkable. If you happen to find a bug, you can submit a report to a
mailing list that the core developers read. Depending on its severity, it is likely that
the bug will be addressed within a couple of hours to a couple of days.
When PHP was put together, it was done so in a modular fashion. This makes
adding greater functionality reasonably easy. If there are sets of functions youâ€™d like
added to PHP, thereâ€™s a good chance that someone can do it with minimal effort.
YOUR PEERS CAN SUPPORT YOU
Most languages have active mailing lists and development sites. PHP is no excep-
tion. If you run into trouble â€” if thereâ€™s a bug in your code that you just canâ€™t figure
out or if you canâ€™t seem to fathom some function or another â€” someone among the
hundreds subscribed to PHP mailing lists will be happy to check and fix your code.
The open-source nature of PHP creates a real feeling of community. When you
get into trouble, your PHP-hacking brethren will feel your pain and ease it.
If you have a computer, Linux, Apache, and PHP are all completely free.
This one is perhaps a little tougher to answer. Although MySQL has much to rec-
ommend it, it also has a variety of competitors, many of whom may be better suited
for a particular task.
In Part I of this book we discuss MySQL in some detail. In these chapters we
mention features available in other relational databases that MySQL does not sup-
port. (If you know your way around databases and are curious, these include stored
procedures, triggers, and subqueries.)
Given these limitations, MySQL is definitely not the best choice in certain envi-
ronments. If you are planning on starting, for example, a bank or a savings and
loan, MySQL probably isnâ€™t for you.
But for the majority of people using the majority of applications, MySQL is a
great choice. It is particularly well suited for Web applications.
Think you need an Oracle installation? Get ready to shell out tens of thousands of
dollars at a minimum. Thereâ€™s no doubt that Oracle, Sybase, and Informix create
terrific databases, but the cost involved is prohibitive for many.
MySQL is free for development and can be used in a live production environ-
ment for a minimal cost (see https://order.mysql.com/index.php?infopage=1
for more details on licensing MySQL).
ITâ€™S QUICK AND POWERFUL
MySQL might not have every bell and whistle available for a relational database,
but for most users it has plenty. If you are serving out Web content or creating a
moderately sized commerce site, MySQL has all the power you need.
For small to-medium-sized databases, MySQL is extremely fast. The developers
of MySQL take great pride in the speed of their product. For applications like the
ones presented in Parts III and IV of this book, it is unlikely youâ€™ll find a database
thatâ€™s any faster.
ITâ€™S IMPROVING ALL THE TIME
MySQL is improving at a staggering rate. The developers release updates frequently
and are adding impressive (and we do mean impressive) features all the time. Itâ€™s
even possible that at the time youâ€™re reading this book MySQL will support sub-
queries and stored procedures.
Your First Application
Enough of the prelude. Now we turn to writing an application so you can see how
all these parts come together in a real live application. By the time you have fin-
ished reading this introduction, you should have a pretty good idea of how it all
You need a few key elements to get going. We run through them here so you know
what you need.
This is a Web-based application, so youâ€™re clearly going to need a Web server. You
will probably be using Apache, whether you are using Windows or Unix. You need
to install Apache so that it can access the PHP language.
In addition, you need to have MySQL installed. And PHP has to be able to rec-
ognize MySQL. Apache, MySQL, and PHP are provided on the accompanying CD,
and installation instructions are provided in Appendix C. You might want to install
these packages before proceeding, or you can just read along to get an idea of what
weâ€™re doing and install the packages later when you want to work with the more
practical examples in this book.
TEXT EDITOR/INTEGRATED DEVELOPMENT ENVIRONMENT
To code PHP and your Web pages, you need, at a minimum, a text editor. You can
use Notepad or something similarly basic, but if youâ€™re starting without an alle-
giance to any particular editor, we suggest you get something with good syntax
highlighting. On Windows, Macromedia HomeSite (www.macromedia.com) is a tool
that works well with PHP, and weâ€™ve heard excellent things about Editplus
If you have been working on Unix for some time, it is likely that you already
know and love some text editor or another, whether it be Emacs, vi, or Kedit. If not,
any of these are fine, though the first two do take some getting used to. If youâ€™re
working on Unix, but donâ€™t have the patience to learn vi, try Pico. Itâ€™s very easy
In the last couple of years, a few companies have released integrated develop-
ment environments (IDEs) for use with PHP. Zend Technologies (www.zend.com),
whose employees have been deeply involved with PHP for years, sells a suite of
products that can make your PHP coding life much easier. Based on code that was
originally open source, NuSphere Corporation (www.nusphere.com) has also cre-
ated a very competent IDE for Windows and Linux.
We start this book with an example of a simple Web application that stores user
information, a place where users can enter their names, email addresses, URLs, and
maybe even comments â€” in essence, a guestbook.
The guestbook is a simplified example, something you would never want to
run on a live Web server. We re-create this application in a more robust form
in Chapter 8.
Creating the database
Now that you know exactly what you need, the first step is to create a database that
stores this information. To do this, you use the language common to most every
database server: SQL. You read a lot more about this later, so donâ€™t worry if you donâ€™t
understand everything right away. Just read through the rest of the Introduction
and then read Chapter 1.
Start up the MySQL command-line client. If youâ€™re working on Unix, typing
mysql at the shell should do the trick (or you might have to go to the directory that
contains the MySQL executable â€” typically /mysql/bin or /usr/local/mysql/bin).
If you are on Windows, you need to go to the DOS prompt, find the path to
mysql.exe, and execute it. Then, at the prompt, create a new database. When youâ€™re
done, you should have something that looks very much like this:
[jay@mybox jay]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 716 to server version: 4.0.1-log