MySQL/PHP Database
Applications
3537-4 FM.f.qc 12/15/00 15:31 Page i
3537-4 FM.f.qc 12/15/00 15:31 Page ii
MySQL/PHP Database
Applications
Jay Greenspan and Brad Bulger
M&T Books
An imprint of IDG Books Worldwide, Inc.
Foster City, CA
●
Chicago, IL
●
Indianapolis, IN
●
New York, NY
3537-4 FM.f.qc 12/15/00 15:31 Page iii
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST
EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR
WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND
SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS
PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN
SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE
OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS,
AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL.
NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER
COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER
DAMAGES.
Trademarks: All brand names and product names used in this book are trade names, service marks, trademarks, or
registered trademarks of their respective owners. IDG Books Worldwide is not associated with any product or vendor
IDG Norge Books for Norway; by IDG Sweden Books for
Sweden; by IDG Books Australia Publishing Corporation
Pty. Ltd. for Australia and New Zealand; by TransQuest
Publishers Pte Ltd. for Singapore, Malaysia, Thailand,
Indonesia, and Hong Kong; by Gotop Information Inc.
for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft
or South Africa; by Eyrolles for France; by International
Thomson Publishing for Germany, Austria, and
Switzerland; by Distribuidora Cuspide for Argentina; by
LR International for Brazil; by Galileo Libros for Chile; by
Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer
Publishing Corporation, Inc., for the Philippines; by
Contemporanea de Ediciones for Venezuela; by Express
Computer Distributors for the Caribbean and West Indies;
by Micronesia Media Distributor, Inc. for Micronesia; by
Chips Computadoras S.A. de C.V. for Mexico; by Editorial
Norma de Panama S.A. for Panama; by American
Bookshops for Finland.
For general information on IDG Books Worldwide’s
books in the U.S., please call our Consumer Customer
Service department at 800-762-2974. For reseller
information, including discounts and premium sales,
please call our Reseller Customer Service department
at 800-434-3422.
For information on where to purchase IDG Books
Worldwide’s books outside the U.S., please contact our
International Sales department at 317-572-3993 or fax
317-572-4002.
For consumer information on foreign language
translations, please contact our Customer Service
Eleventh Annual
Computer Press
Awards 1995
Tenth Annual
Computer Press
Awards 1994
Eighth Annual
Computer Press
Awards 1992
Ninth Annual
Computer Press
Awards 1993
IDG is the world’s leading IT media, research and exposition company. Founded in 1964, IDG had 1997 revenues of $2.05
billion and has more than 9,000 employees worldwide. IDG offers the widest range of media options that reach IT buyers
in 75 countries representing 95% of worldwide IT spending. IDG’s diverse product and services portfolio spans six key areas
including print publishing, online publishing, expositions and conferences, market research, education and training, and
global marketing services. More than 90 million people read one or more of IDG’s 290 magazines and newspapers, including
IDG’s leading global brands — Computerworld, PC World, Network World, Macworld and the Channel World family of
publications. IDG Books Worldwide is one of the fastest-growing computer book publishers in the world, with more than
700 titles in 36 languages. The “ For Dummies
®
” series alone has more than 50 million copies in print. IDG offers online
users the largest network of technology-specific Web sites around the world through IDG.net (), which
comprises more than 225 targeted Web sites in 55 countries worldwide. International Data Corporation (IDC) is the world’s
largest provider of information technology data, analysis and consulting, with research centers in over 41 countries and more
than 400 research analysts worldwide. IDG World Expo is a leading producer of more than 168 globally branded conferences
and expositions in 35 countries including E3 (Electronic Entertainment Expo), Macworld Expo, ComNet, Windows World
Expo, ICE (Internet Commerce Expo), Agenda, DEMO, and Spotlight. IDG’s training subsidiary, ExecuTrain, is the world’s
largest computer training company, with more than 230 locations worldwide and 785 training courses. IDG Marketing
Services helps industry-leading IT companies build international brand recognition by developing global integrated marketing
Chairman and CEO
IDG Books Worldwide, Inc.
3537-4 FM.f.qc 12/15/00 15:31 Page v
About the Authors
Jay Greenspan made his living as a technical consultant and editor before finding
his way into Wired Digital’s Webmonkey. There he learned everything he knows
about Web technology and gained an appreciation for electronic music, the color
orange, and a “cute top.” He now makes his living as a writer and consultant. He
will neither confirm nor deny the rumors that he once worked for a prime-time
game show.
Brad Bulger can remember when computers were as big as refrigerators and old-
timers would come into the machine room and call them “mini.” He learned more
than anyone really should about database systems by working for Relational
Technology nee Ingres nee CA for many years. After an interregnum, he got a job
with Wired. He would still like to know when the future is going to get here, but has
a sneaking suspicion he already knows.
Credits
ACQUISITIONS EDITOR
Debra Williams Cauley
PROJECT EDITOR
Neil Romanosky
TECHNICAL EDITORS
Richard Lynch
Michael Widenius
COPY EDITOR
S. B. Kleinman
PROJECT COORDINATORS
Louigene A. Santos
Danette Nurse
GRAPHICS AND PRODUCTION
writing Web-based applications with PHP and MySQL. If you decide to go with
these tools, you’ll be in excellent company. Thousands of developers — from total
newbies to programmers with years of experience —are turning to PHP and MySQL
for their Web-based projects; and for good reason.
Both PHP and MySQL are easy to use, fast, free, and powerful. If you want to get
a dynamic Web site up quickly, there are no better choices. The PHP scripting lan-
guage was built for the Web. All the tasks common to Web development can be
accomplished in PHP with an absolute minimum of effort. Similarly, MySQL excels at
tasks common to dynamic Web sites. Whether you’re creating a content-management
system or an e-commerce application, MySQL is a great choice for your data storage.
Is This Book for You?
There are quite a few books that deal with PHP and a few that cover MySQL. We’ve
read some of these and found a few to be quite helpful. If you’re looking for a book
that deals with gory details of either of these packages, you should probably look
elsewhere.
The focus of this book is applications development. We are concerned with what
it takes to get data-driven Web sites up and running in an organized and efficient
way. The book does not go into arcane detail of every aspect of either of these tools.
For example, in this book, you will not find a discussion of PHP’s LDAP functions
or MySQL’s C application program interface (API). Instead, we will focus on the
pieces of both packages that affect one another. We hope that by the time you’re
done with this book you’ll know what it takes to get an application up and running
using PHP and MySQL.
How This Book Is Organized
We have organized the book into four parts.
Part I: Using MySQL
Before you code any PHP scripts, you will need to know how to design a database,
create tables in your database, and get the information you want from the database.
Part I of this book will show you about all you need to know to work with MySQL.
ix
x Preface
3537-4 FM.f.qc 12/15/00 15:31 Page x
Acknowledgments
This book would never have happened if not for the efforts of Debra Williams
Cauley. I thank her for her patience and persistence. The efforts and talents of Neil
Romanosky, S. B. Kleinman, and many others at IDG Books have made this book
more lucid and attractive than we could have hoped. Richard Lynch’s exacting eye
and technical acumen kept our code clean, fast, and readable.
Any book on open-source software owes debt to those who have created these
great tools. So I thank everyone involved with PHP and MySQL, from the core
developers to those who contribute to the documentation. Special thanks to
Michael (Monty) Widenius, MySQL’s lead developer. He has not only created a ter-
rific relational database, but has offered his advice and expertise to the authors of
this book.
xi
3537-4 FM.f.qc 12/15/00 15:31 Page xi
Contents at a Glance
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Part I Working with MySQL
Chapter 1 Database Design with MySQL . . . . . . . . . . . . . . . . . 3
Chapter 2 The Structured Query Language for Creating and
Altering Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 3 Getting What You Want with select . . . . . . . . . . . . 45
Part II Working with PHP
Chapter 4 Getting Started with PHP —Variables . . . . . . . . . . . 71
Chapter 5 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter 6 PHP’s Built-in Functions . . . . . . . . . . . . . . . . . . . 111
Chapter 7 Writing Organized and Readable Code . . . . . . . . . 165
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Part I Working with MySQL
Chapter 1 Database Design with MySQL . . . . . . . . . . . . . . . . . . . . . 3
Why Use a Relational Database? . . . . . . . . . . . . . . . . . . . . . . 3
Blasted Anomalies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Update anomaly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Delete anomaly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Insert anomaly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Normalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1st normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2nd normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3rd normal form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Types of Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
One-to-many relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
One-to-one relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Many-to-many relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Features MySQL Does Not Support . . . . . . . . . . . . . . . . . . . . 17
Referential integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Stored procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 2 The Structured Query Language for Creating and
Altering Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
create database Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 24
use database Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
create table Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Column Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Correlated subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Part II Working with PHP
Chapter 4 Getting Started with PHP —Variables. . . . . . . . . . . . . . 71
Assigning Simple Variables Within a Script . . . . . . . . . . . . . 71
Delimiting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Assigning arrays within a script. . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Assigning two-dimensional arrays in a script . . . . . . . . . . . . . . . . 76
Accessing Variables Passed from the Browser. . . . . . . . . . . . 77
HTML forms variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Passing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Cookies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Using Built-In Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
PHP variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Apache variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
xvi Contents
3537-4 FM.f.qc 12/15/00 15:31 Page xvi
Other Web server variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Testing Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
isset() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
empty(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
is_int() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
is_double() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
is_string() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
is_array() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
is_bool() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
is_object(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
gettype(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
MySQL API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
String-handling functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Regular expression functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Contents xvii
3537-4 FM.f.qc 12/15/00 15:31 Page xvii
Type-conversion functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Array functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Print functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Date/time functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Filesystem functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Random number generator functions. . . . . . . . . . . . . . . . . . . . . . 157
cURL functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Session functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
HTTP header functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Mail function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
URL functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Output buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Chapter 7 Writing Organized and Readable Code. . . . . . . . . . . . 165
Indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Code blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Function calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
SQL statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
include() and require(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
include_once() and require_once() . . . . . . . . . . . . . . . . . . . . . . . . 171
User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Function basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Returning values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Using a variable number of arguments . . . . . . . . . . . . . . . . . . . . 177
Variable scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
admin_get_winner.php. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
admin_winners.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Part IV Not So Simple Applications
Chapter 10 Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Determining the Scope and Goals of the Application . . . . . 250
Necessary Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
What Do We Need to Prevent? . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
A flawed data design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
MySQL oddities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
A better schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
The object-oriented approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Accessing the filesystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Uploading files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Accessing outside utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Code Breakdown. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Objects in theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Objects in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Sample Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Chapter 11 Content Management System . . . . . . . . . . . . . . . . . . . 285
Determining the Scope and Goals of the Application . . . . . 286
Necessary pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
What do you need to prevent? . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Designing the Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Code Breakdown. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Reusable functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Interesting Code Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Configuration Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Configuring for encryption and security . . . . . . . . . . . . . . . . . . . 369
Encryption and security tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Configuring for credit-card authorization . . . . . . . . . . . . . . . . . . 372
Configuring for session handling . . . . . . . . . . . . . . . . . . . . . . . . . 372
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Session functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
cURL functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Dealing with the credit-card processor. . . . . . . . . . . . . . . . . . . . . 377
Code Breakdown. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
xx Contents
3537-4 FM.f.qc 12/15/00 15:31 Page xx
Part V Appendixes
Appendix A HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Appendix B Brief Guide to PHP/MySQL Installation and
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Appendix C MySQL Utilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Appendix D MySQL User Administration . . . . . . . . . . . . . . . . . . . . 439
Appendix E PHP Function Reference . . . . . . . . . . . . . . . . . . . . . . . . 447
Appendix F Regular Expressions Overview. . . . . . . . . . . . . . . . . . . 507
Appendix G Helpful User-Defined Functions. . . . . . . . . . . . . . . . . . 517
Appendix H PHP and MySQL Resources. . . . . . . . . . . . . . . . . . . . . . 543
Appendix I MySQL Function Reference. . . . . . . . . . . . . . . . . . . . . . 551
Appendix J What’s on the CD-ROM. . . . . . . . . . . . . . . . . . . . . . . . . 585
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
End-User License Agreement . . . . . . . . . . . . . . . . . . . 599
GNU General Public License . . . . . . . . . . . . . . . . . . . . . 602
CD-ROM Installation Instructions . . . . . . . . . . . . . . . . 608
stated, that means that both a central server and a client application are responsi-
ble for some amount of processing. This differs from a program such as Microsoft
Word, which operates just fine without any help from a server. Those of you who
used older VAX machines will remember the days of dumb terminals, which had no
processing power whatsoever. Depending on where you work today, perhaps in a
university or a bank, you may still use applications that are in no way dependent
on the client. In other words, all the work is done on the central computer.
N
O
TE
3537-4 FM.f.qc 12/15/00 15:31 Page xxiii
The client
The applications you can develop with MySQL and PHP make use of a single client:
the Web browser. This is not the only possibility for Internet-based applications.
For very sophisticated applications that require more client-side processing or that
need to maintain state (we will talk about maintaining state later in the Introduc-
tion), a Java applet may be necessary. But unless you’re coding something like a
real-time chat program, client-side Java is completely unnecessary.
So the only client you should be concerned with is the Web browser. The appli-
cations will need to render in the browser. As you probably already know, the pri-
mary language of browsers is the hypertext markup language or HTML. HTML
provides a set of tags that describe how a Web page should look. If you are new to
the concept of HTML, get on the Web and read one of the many tutorials out there.
It shouldn’t take that much time to learn the basics.
Of course, most browsers will accept more than HTML. There are all kinds of
plug-ins, including RealPlayer, Flash, and Shockwave. Most browsers also have
some level of support for JavaScript, and some of the newer ones can work with
XML. But, like most Web developers, we will be taking a lowest-common-denomi-
nator approach in this book. We’re going to create applications that can be read in
any browser. There will be no JavaScript, XML, or anything else that could prevent
see it. They make use of an open-source development model, which allows anyone
who is interested to participate in the development of the project. In the case of
PHP, coders all over the world participate in the development of the language and
see no immediate pay for their substantial work. Most of the people who participate
are passionate about good software and code for the enjoyment of seeing people
like you and me develop with their tools.
This method of development has been around for some time, but it has gained
prominence as Linux has become increasingly popular. More often than not, open-
source software is free. You can download the application, install it, and use it
without getting permission from anyone or paying a dime to anyone.
Suffice it to say that Microsoft, Oracle, and other traditional software companies
do not make use of this method of development.
If you are not an open-source zealot, there are excellent reasons to choose
NT/2000. Usually, the thing that steers people towards NT/2000 is inertia. If you or
your company has been developing with Microsoft products for years, it is probably
going to be easier to stay within that environment. If you have a team of people who
Web Browser
(Internet Explorer,
N
etscape)
Internet
Web Server
(Apache,IIS)
Middleware
PHP, ColdFusion,
ASP,JSP
Relational
Database
(MySQL, Oracle, MS
SQL)
the Microsoft way, you’ll almost certainly end up using IIS.
There is a certain amount of integration between the programming language and
Web server. At this point, PHP 4 integrates well with IIS. As of this writing, there is
some concern about the stability of PHP/IIS under heavy load, but PHP is improv-
ing all the time, and by the time you read this there may no longer be a problem.
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 will
most often run 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, there are limited graphical tools with which you can manipulate the
xxvi Introduction
3537-4 FM.f.qc 12/15/00 15:31 Page xxvi
application. 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, there are also versions that 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 is the easiest way to go if you need to develop locally on Windows but to serve
off a Unix/Apache server.
MIDDLEWARE
PHP belongs to a class of languages known as middleware. These languages work
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 thing: They all interact with relational databases; they all
work with a filesystem; they all interact with a Web server. The question about
which language is best is rarely a matter of a language’s inability to perform cer-
tain actions. It’s usually more a matter of how quickly you can do what you need
to do with the least amount of pain.
IT’S FAST AND EASY
What about speed? There are really only three things that we know for sure when it
comes to comparing speeds of Web programming languages. First, applications
written in C will be the fastest. Second, programming in C is rather difficult and
will take much longer than any of the other languages mentioned so far. Third,
comparisons between languages are extremely difficult. From everything we know,
we feel safe in saying the PHP is as fast as anything out there.
More often than not choosing a language comes back to the same issues
involved in buying shoes. You’ll want to go with what’s most comfortable. If you’re
like us, you will find that PHP has managed the perfect mix of power, structure, and
ease of use. Again, this is largely a matter of opinion, but we do believe the syntax
of PHP is superior to that 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 the best opportunity to develop powerful Web
applications quickly. That generalization made, we do believe there are other excel-
lent reasons for choosing PHP.
IT’S CROSS-PLATFORM
In the rundown of Web architecture, we mentioned that PHP will run on Windows
2000/NT 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 will work with it.
Yes, ASP can be run on Linux, and ColdFusion can work on Solaris and Linux,