Welcome to the world of IDG Books Worldwide. IDG Books Worldwide, Inc., is a subsidiary of International Data Group, the world’s largest publisher of computer-related information and the leading global provider of information services on information technology. IDG was founded more than 30 years ago by Patrick J. McGovern and now employs more than 9,000 people worldwide. IDG publishes more than 290 computer publications in over 75 countries. More than 90 million people read one or more IDG publications each month. Launched in 1990, IDG Books Worldwide is today the #1 publisher of best-selling computer books in the United States. We are proud to have received eight awards from the Computer Press Association in recognition of editorial excellence and three from Computer Currents’ First Annual Readers’ Choice Awards. Our best- selling ...For Dummies® series has more than 50 million copies in print with translations in 31 languages. IDG Books Worldwide, through a joint venture with IDG’s Hi-Tech Beijing, became the first U.S. publisher to publish a computer book in the People’s Republic of China. In record time, IDG Books Worldwide has become the first choice for millions of readers around the world who want to learn how to better manage their businesses. Our mission is simple: Every one of our books is designed to bring extra value and skill-building instructions to the reader. Our books are written by experts who understand and care about our readers. The knowledge base of our editorial staff comes from years of experience in publishing, education, and journalism — experience we use to produce books to carry us into the new millennium. In short, we care about books, so we attract the best people. We devote special attention to details such as audience, interior design, use of icons, and illustrations. And because we use an efficient process of authoring, editing, and desktop publishing our books electronically, we can spend more time ensuring superior content and less time on the technicalities of making books. You can count on our commitment to deliver high-quality books at competitive prices on topics you want to read about. At IDG Books Worldwide, we continue in the IDG tradition of delivering quality for more than 30 years. You’ll find no better book on a subject than one from IDG Books Worldwide. John Kilcullen Chairman and CEO IDG Books Worldwide, Inc. Eighth Annual Eleventh Annual Computer Press Computer Press Awards 1992 Ninth Annual Tenth Annual Awards 1995 Computer Press Computer Press Awards 1993 Awards 1994IDG is the world’s leading IT media, research and exposition company. Founded in 1964, IDG had 1997 revenues of $2.05billion and has more than 9,000 employees worldwide. IDG offers the widest range of media options that reach IT buyersin 75 countries representing 95% of worldwide IT spending. IDG’s diverse product and services portfolio spans six key areasincluding print publishing, online publishing, expositions and conferences, market research, education and training, andglobal marketing services. More than 90 million people read one or more of IDG’s 290 magazines and newspapers, includingIDG’s leading global brands — Computerworld, PC World, Network World, Macworld and the Channel World family ofpublications. IDG Books Worldwide is one of the fastest-growing computer book publishers in the world, with more than700 titles in 36 languages. The “...For Dummies®” series alone has more than 50 million copies in print. IDG offers onlineusers the largest network of technology-specific Web sites around the world through IDG.net (http://www.idg.net), whichcomprises more than 225 targeted Web sites in 55 countries worldwide. International Data Corporation (IDC) is the world’slargest provider of information technology data, analysis and consulting, with research centers in over 41 countries and morethan 400 research analysts worldwide. IDG World Expo is a leading producer of more than 168 globally branded conferencesand expositions in 35 countries including E3 (Electronic Entertainment Expo), Macworld Expo, ComNet, Windows WorldExpo, ICE (Internet Commerce Expo), Agenda, DEMO, and Spotlight. IDG’s training subsidiary, ExecuTrain, is the world’slargest computer training company, with more than 230 locations worldwide and 785 training courses. IDG MarketingServices helps industry-leading IT companies build international brand recognition by developing global integrated marketingprograms via IDG’s print, online and exposition products worldwide. Further information about the company can be foundat www.idg.com. 1/26/00
PrefaceWelcome. If you are thumbing through these pages, you’re probably consideringwriting Web-based applications with PHP and MySQL. If you decide to go withthese tools, you’ll be in excellent company. Thousands of developers — from totalnewbies to programmers with years of experience — are turning to PHP and MySQLfor their Web-based projects; and for good reason. Both PHP and MySQL are easy to use, fast, free, and powerful. If you want to geta 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 beaccomplished in PHP with an absolute minimum of effort. Similarly, MySQL excels attasks common to dynamic Web sites. Whether you’re creating a content-managementsystem 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’veread some of these and found a few to be quite helpful. If you’re looking for a bookthat deals with gory details of either of these packages, you should probably lookelsewhere. The focus of this book is applications development. We are concerned with whatit takes to get data-driven Web sites up and running in an organized and efficientway. 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 functionsor MySQL’s C application program interface (API). Instead, we will focus on thepieces of both packages that affect one another. We hope that by the time you’redone with this book you’ll know what it takes to get an application up and runningusing PHP and MySQL.How This Book Is OrganizedWe have organized the book into four parts.Part I: Using MySQLBefore 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 Part II: Using PHP As an applications developer, the bulk of your time will be spent writing scripts that access the database and present HTML to a user’s browser. Part II will start by showing you the basics of the PHP scripting language, covering how PHP works with variables, conditions, and control structures. Part II will also cover many of PHP’s functions and discuss techniques for writing clean, manageable code. Part III: Simple Applications In this part, we present two of the seven applications in this book: a guestbook and a survey. Here you will see the lessons from Parts I and II put into practice as we build working applications. Part IV: Not So Simple Applications Here the applications will be more complex, as we present applications commonly used on the Web. You will see how you can design a content management system, a discussion board, a shopping cart, and other useful applications. Along the way, we will show some tips and techniques that should be helpful as you write your applications. Part V: Appendixes The appendixes cover several topics of interest to the MySQL/PHP developer. In the appendixes, you will find installation and configuration instructions, quick refer- ence guides to PHP and MySQL functions, a regular expressions overview, and guides to MySQL administration. In addition, there are a few helpful resources, snippets of code, and instructions on using the CD-ROM. Tell Us What You Think Both the publisher and authors of this book hope you find it a valuable resource. Please feel free to register this book at the IDG Books Web site (http://www. idgbooks.com) and give us your feedback. Also check in at the site we’ve dedicated to this book, http://www.mysqlphpapps.com/, where you will be able to contact the authors and find updates to the applications created for this book.
AcknowledgmentsThis book would never have happened if not for the efforts of Debra WilliamsCauley. I thank her for her patience and persistence. The efforts and talents of NeilRomanosky, S. B. Kleinman, and many others at IDG Books have made this bookmore lucid and attractive than we could have hoped. Richard Lynch’s exacting eyeand technical acumen kept our code clean, fast, and readable. Any book on open-source software owes debt to those who have created thesegreat tools. So I thank everyone involved with PHP and MySQL, from the coredevelopers to those who contribute to the documentation. Special thanks toMichael (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 ofthis book. xi
Introduction xxv Relational Database (MySQL, Oracle, MS SQL) Middleware Web Server PHP, ColdFusion, (Apache,IIS) ASP,JSP Internet Web Browser (Internet Explorer, Netscape)Figure I-1: Architecture of Web applications Which should you use? Well, this is a complex question, and the answer formany will be based partially on religion. In case you’re unaware of it, let’s take amoment to talk about the broad topics in this religious war. If you don’t know what we are talking about, here are the basics. PHP andMySQL belong to a class of software known as open source. This means that thesource code to the heart of their applications is available to anyone who wants tosee it. They make use of an open-source development model, which allows anyonewho is interested to participate in the development of the project. In the case ofPHP, coders all over the world participate in the development of the language andsee no immediate pay for their substantial work. Most of the people who participateare passionate about good software and code for the enjoyment of seeing peoplelike you and me develop with their tools. This method of development has been around for some time, but it has gainedprominence as Linux has become increasingly popular. More often than not, open-source software is free. You can download the application, install it, and use itwithout getting permission from anyone or paying a dime to anyone. Suffice it to say that Microsoft, Oracle, and other traditional software companiesdo not make use of this method of development. If you are not an open-source zealot, there are excellent reasons to chooseNT/2000. Usually, the thing that steers people towards NT/2000 is inertia. If you oryour company has been developing with Microsoft products for years, it is probablygoing to be easier to stay within that environment. If you have a team of people who
xxvi Introduction know Visual Basic, you are probably going to want to stick with NT/2000. Even if this is the case, there’s nothing to prevent you from developing with PHP and MySQL. Both products run on Windows 95/98 and Windows NT/2000. But in the real world, almost all PHP/MySQL applications are running off of some version of Unix, whether it be Linux, BSD, Irix, Solaris, HP-UX, or one of the other flavors. For that reason, the applications in this book will work with Unix. If you need to run these on Windows, minor alterations to the PHP scripts may be necessary. Most of the people who created PHP and MySQL are deeply involved with Unix, and most of their development is done on Unix machines, so it’s not surprising that the software they have created works best on Linux, BSD, and other Unix boxes. The major advantage of Unix is its inherent stability. Boxes loaded with Linux have been known to run months or years without crashing. Linux and BSD also have the advantage of being free 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 this on the Web, where a moderate amount of traffic might overwhelm it, but it will serve nicely as a development server, a place where