Beginning PHP6, Apache, MySQL ® Web Development www.wrox.com $49.99 USA $59.99 CAN Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing a structured, tutorial format that will guide you through all the techniques involved. Recommended Computer Book Categories PHP, Database Management Web Page Design ISBN: 978-0-470-39114-3 With this guide, you’ll quickly learn why the combination of PHP, Apache, and MySQL is rapidly becoming the most popular way to develop dynamic web sites. It gives you the best possible foundation for understanding how the core components work separately and together, enabling you to take full advantage of everything they have to offer. You’ll discover how to utilize the key features of these technologies as you follow two projects to create complete web sites. These projects take you through the basics, such as writing PHP code, building a MySQL database, filling the database with data, and showing specific information to your visitors. You’ll then incorporate some of the more complex topics of working with PHP, Apache, and MySQL as you progress step by step through the development of each site. When you’ve finished this book, you’ll have a thorough understanding of the core concepts you need to become an effective developer. Plus you’ll be able to create a well-designed, dynamic web site using freely available tools. What you will learn from this book ● Installation and configuration of PHP, Apache, and MySQL ● Ways to avoid errors and how to handle them when they occur ● Techniques for creating, altering, and working with image files ● Steps for building a content management system ● How to monitor your web site through activity logs and error logs ● Setting up e-mail lists and handling user registrations ● Tips for adding e-commerce capabilities ● How to connect to MySQL from PHP Who this book is for This book is for PHP beginners who have some experience with web site development concepts and a basic working knowledge of HTML and CSS. Enhance Your Knowledge Advance Your Career PHP6, Apache, MySQL ® Web Development Boronczyk, Naramore, Gerner, Le Scouarnec, Stolz, Glass Beginning spine=1.68" Updates, source code, and Wrox technical support at www.wrox.com Beginning PHP6, Apache, MySQL ® Web Development Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass Wrox Programmer to Programmer TM Wrox Programmer to Programmer TM Beginning PHP6, Apache, MySQL ® Web Development Enhance Your Knowledge Advance Your Career Professional Web APIs with PHP 978-0-7645-8954-6 This book is for programmers who have a strong understanding of PHP and are looking for detailed coverage of multiple APIs in PHP. Professional Joomla! 978-0-470-13394-1 This book is for web developers, hobbyists, web designers, blog- gers, corporate content creators, and support specialists who are looking to build a more robust web site. Basic skills in the area of PHP programming are necessary. PHP and MySQL Create-Modify-Reuse 978-0-470-19242-9 This book is for anyone who is familiar with the fundamentals of programming in PHP and MySQL and is interested in program- ming a variety of applications. Beginning PHP6, Apache, MySQL Web Development 978-0-470-39114-3 This book is for the PHP beginners who have some experience with web site development concepts and a basic working knowl- edge of HTML and CSS. Beginning CSS: Cascading Style Sheets for Web Design, 2nd Edition 978-0-470-09697-0 This book discusses how to style XML documents with CSS—XML being a more advanced markup language with multipurpose applications. XML will play an increasingly larger role in the production of XHTML documents in the future. Beginning MySQL 978-0-7645-7950-9 This book is for programmers who are new to MySQL but who have some experience in PHP, Java, or ASP/ASP.NET developing applications that access backend databases. spine=tk" Get more out of WROX.com Programmer to Programmer ™ Interact Take an active role online by participating in our P2P forums Wrox Online Library Hundreds of our books are available online through Books24x7.com Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble! Chapters on Demand Purchase individual book chapters in pdf format Join the Community Sign up for our free monthly newsletter at newsletter.wrox.com Browse Ready for more Wrox? We have books and e-books available on .NET, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more! Contact Us. We always like to get feedback from our readers. Have a book idea? Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com Beginning PHP6, Apache, MySQL ® Web Development Introduction xxiii Part I: Movie Review Web Site Chapter 1: Configuring Your Installation 3 Chapter 2: Creating PHP Pages Using PHP6 19 Chapter 3: Using PHP with MySQL 77 Chapter 4: Using Tables to Display Data 105 Chapter 5: Form Elements: Letting the User Work with Data 131 Chapter 6: Letting the User Edit the Database 153 Chapter 7: Manipulating and Creating Images with PHP 175 Chapter 8: Validating User Input 217 Chapter 9: Handling and Avoiding Errors 241 Part II: Comic Book Fan Site Chapter 10: Building Databases 263 Chapter 11: Sending E-mail 315 Chapter 12: User Logins, Profiles, and Personalization 355 Chapter 13: Building a Content Management System 407 Chapter 14: Mailing Lists 469 Chapter 15: Online Stores 505 Chapter 16: Creating a Bulletin Board System 557 Chapter 17: Using Log Files to Improve Your Site 627 Chapter 18: Troubleshooting 641 Appendix A: Answers to Exercises 649 Appendix B: PHP Quick Reference 685 (continued) ffirs.indd iffirs.indd i 12/12/08 10:48:19 AM12/12/08 10:48:19 AM Appendix C: PHP6 Functions 695 Appendix D: MySQL Data Types 753 Appendix E: MySQL Quick Reference 757 Appendix F: Comparison of Text Editors 761 Appendix G: Choosing a Third-Party Host 765 Appendix H: An Introduction to PHP Data Objects 769 Appendix I: Installation and Configuration on Linux 777 Index 785 ffirs.indd iiffirs.indd ii 12/12/08 10:48:20 AM12/12/08 10:48:20 AM Beginning PHP6, Apache, MySQL ® Web Development ffirs.indd iiiffirs.indd iii 12/12/08 10:48:20 AM12/12/08 10:48:20 AM ffirs.indd ivffirs.indd iv 12/12/08 10:48:20 AM12/12/08 10:48:20 AM Beginning PHP6, Apache, MySQL ® Web Development Timothy Boronczyk Elizabeth Naramore Jason Gerner Yann Le Scouarnec Jeremy Stolz Michael K. Glass Wiley Publishing, Inc. ffirs.indd vffirs.indd v 12/12/08 10:48:21 AM12/12/08 10:48:21 AM Beginning PHP6, Apache, MySQL ® Web Development Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-39114-3 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data. Beginning PHP6, Apache, MySQL web development / Timothy Boronczyk . . . [et al.]. p. cm. Includes index. ISBN 978-0-470-39114-3 (paper/website) 1. Web sites—Design. 2. Apache (Computer file : Apache Group) 3. PHP (Computer program language) 4. MySQL (Electronic resource) I. Boronczyk, Tim, 1979- TK5105.888.B426 2009 006.7'8—dc22 2008047012 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. MySQL is a registered trademark of MySQL AB. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. ffirs.indd viffirs.indd vi 12/12/08 10:48:21 AM12/12/08 10:48:21 AM About the Authors Timothy Boronczyk is a native of Syracuse, New York, where he works as a programmer by day and a freelance developer, writer, and technical editor by night. He has been involved in web design since 1998, and over the years has written several articles on PHP programming and various design topics, as well as the book PHP and MySQL: Create - Modify - Reuse (Wrox). Timothy holds a degree in software application programming, is a Zend Certified Engineer, and recently started his first business venture, Salt City Tech ( www.saltcitytech.com ). In his spare time, he enjoys photography, hanging out with friends, and sleeping with his feet hanging off the end of his bed. He ’ s easily distracted by shiny objects. Elizabeth Naramore graduated from Miami University (Ohio) with a degree in organizational behavior and has been a web developer since 1997. Her main focus is in e - commerce, but she develops sites across numerous industries. She is currently a moderator at PHPBuilder.com, an online help center for PHP. She lives in Cincinnati, Ohio, with her husband and two children, and looks forward to someday returning to Miami U. to get her masters in computer science. Jason “ Goldbug ” Gerner currently spends his days working as a web developer in Cincinnati and burns free time complaining about lack of support for web standards and abusing XML. He can often be found lurking in the PHPBuilder.com discussion forums, waiting to chime in with nagging comments about CSS or code efficiency. Yann “ Bunkermaster ” Le Scouarnec is the senior developer for Jolt Online Gaming, a British gaming company. He is a moderator at PHPBuilder.com and a developer of open source PHP software for the gaming community. He has also worked for major software corporations as a software quality expert. Jeremy “ Stolzyboy ” Stolz is a web developer at J & M Companies, Inc. ( www.jmcompanies.com ), a print company in Fargo, North Dakota. Jeremy is primarily a PHP/MySQL developer, but he has also worked with many other languages. When not working, he frequents the Internet and tries to keep his programming skills sharp and up to date. He is a contributor to and moderator at PHPBuilder.com. Michael “ BuzzLY ” Glass has been a gladiator in the software/Web site development arena for more than eight years. He has more than ten years of commercial programming experience with a wide variety of technologies, including PHP, Java, Lotus Domino, and Vignette StoryServer. He divides his time between computer programming, playing pool in the APA, and running his web site at www.ultimatespin.com . You can usually find him slinking around on the PHPBuilder.com forums, where he is a moderator with the nickname BuzzLY. ffirs.indd viiffirs.indd vii 12/12/08 10:48:22 AM12/12/08 10:48:22 AM ffirs.indd viiiffirs.indd viii 12/12/08 10:48:22 AM12/12/08 10:48:22 AM [...]... 777 Index 785 xxii ftoc.indd xxii 12/12/08 10:49:43 AM Introduction Welcome to Beginning PHP6, Apache, MySQL Web Development, your new trusty resource for assistance in creating your own dynamic web sites There are a lot of technologies available that can be used to deliver great web sites, and we’re glad you chose the Apache /MySQL/ PHP (sometimes referred to simply as AMP) approach You may or may not... xxiii Part I: Movie Review Web Site Chapter 1: Configuring Your Installation Projects in This Book A Brief Introduction to Apache, MySQL, PHP, and Open Source A Brief History of Open Source Initiatives Why Open Source Rocks How the AMP Pieces Work Together Installing Apache, MySQL, and PHP on Windows Installing Apache Installing PHP Configuring PHP to Use MySQL Installing MySQL Where to Go for Help... www.ultimatespin.com: Spidon, Kaine, Garmy, Spidermanalf, Ping, Webhead, and FancyDan You guys rock! To Donna and Gerry, who have influenced my life more than they can ever know, and who have taught me the importance of finishing what you’ve started — Michael Glass ffirs.indd xi 12/12/08 10:48:23 AM ffirs.indd xii 12/12/08 10:48:23 AM Beginning PHP6, Apache, MySQL Web Development ffirs.indd xiii 12/12/08 10:48:23 AM... heredoc Incrementing/Decrementing Values OOP Dreams Classes Properties and Methods Why Use OOP? Summary Exercises Chapter 3: Using PHP with MySQL Overview of MySQL Structure and Syntax MySQL Structure MySQL Syntax and Commands How PHP Fits with MySQL Connecting to the MySQL Server Looking at a Ready-Made Database Querying the Database WHERE, oh WHERE Working with PHP and Arrays of Data: foreach A Tale... latest and greatest versions of Apache, MySQL, and PHP at the time of publishing, chances are those versions won’t be around for long It is important for you to visit the source web sites for each component to get the most updated versions and recent release notes We recommend that you always use the most recent stable releases when developing web sites using Apache, MySQL, and PHP Using older software... “dark side” to join us as Apache, MySQL, and PHP enthusiasts! What ’s Covered in the Book A variety of topics are covered in this book: ❑ ❑ flast.indd xxiii Installation and configuration of Apache, MySQL, and PHP Basic introduction to each component and how they interact with one another 12/10/08 6:11:23 PM Introduction ❑ Gathering information from and interacting with your web site visitors ❑ How to... reference materials you’ll need to assist you in your web development journey once you complete the book and propose tools to help make you a more efficient coder After reading this book, you will be able to create a well-designed, dynamic web site using freely available tools c01.indd 3 12/10/08 5:24:17 PM Part I: Movie Review Web Site A Brief Introduction to Apache, My SQL, PHP, and Open Source There are... operating systems Apache, MySQL, and PHP are all open source projects that can be installed on a wide variety of platforms They are most popular on Linux (giving the acronym “LAMP”) although Windows-based Apache, MySQL and PHP installations are becoming increasingly popular, especially for developers The open source movement is a collaboration of some of the finest minds in programming and development, ... information about the OSI and the open source community, visit the OSI web site at www.opensource.org How the AMP Pieces Work Together Now that you’ve learned about some of the spirit and history of open source, it’s important to understand the role Apache, MySQL and PHP play in creating your web site Imagine for a moment that your dynamic web site is like a fancy restaurant Hungry diners come to your place... safe environment to test your code without breaking a live web site Installing Apache, My SQL , and PHP on Windows After following these instructions, you will have successfully installed Apache, MySQL, and PHP on your Windows system We cover installing them on Windows XP– and Windows Vista–based systems You should review each component’s web site if you want more detailed installation instructions . variety of applications. Beginning PHP6, Apache, MySQL Web Development 978-0-470-39114-3 This book is for the PHP beginners who have some experience with web site development concepts and a. Cataloging-in-Publication Data. Beginning PHP6, Apache, MySQL web development / Timothy Boronczyk . . . [et al.]. p. cm. Includes index. ISBN 978-0-470-39114-3 (paper/website) 1. Web sites—Design. 2 Gerner, Le Scouarnec, Stolz, Glass Beginning spine=1.68" Updates, source code, and Wrox technical support at www.wrox.com Beginning PHP6, Apache, MySQL ® Web Development Timothy Boronczyk,