this print for content only—size & color not accurate spine = 2.02" 1,080 page count Books for professionals By professionals ® Beginning PHP and MySQL: From Novice to Professional, THIRD EDITION Dear Reader, PHP and MySQL have long ranked among the world’s most popular technologies for building powerful Web sites, and with little wonder. Both offer developers an amazing array of features, yet have managed to remain very approachable even to the most novice of developers. In the latest edition of this bestselling book, you’ll learn all about PHP and MySQL, setting you along the path toward creating the Web site you’ve long dreamed about. I begin with a vast overview of PHP’s capabilities, starting with a survey of the installation and configuration process on both the Windows and Linux platforms. Next, I devote several chapters to basic PHP concepts, including variables, datatypes, functions, string manipulation, object orientation, and user interaction. I also explain key concepts such as PEAR, session handling, the Smarty templating engine, Web services, and PDO. You’ll also find a new chapter devoted to creating multilingual Web sites, and a new chapter that shows you how to create power- ful Web sites faster using the popular Zend Framework. I’ve also updated the material to introduce key new features found in the PHP 6 release. I then introduce many of MySQL’s key features, beginning by guiding you through MySQL’s installation and configuration process. Next, you’ll learn about MySQL’s storage engines and datatypes, administration utilities, security features, and data import/export facilities. I’ll also introduce you to several advanced features such as triggers, stored procedures, and views. Along the way, you’ll gain insight into PHP’s ability to communicate with MySQL, and learn how to create and execute queries, perform searches, and carry out other key database tasks from within your Web site. W. Jason Gilmore Coauthor of Beginning PHP and PostgreSQL 8: From Novice to Professional Beginning PHP and Oracle: From Novice to Professional US $46.99 Shelve in PHP/Web Development User level: Beginner–Intermediate Gilmore THIRD EDITION PHP and MySQL The eXperT’s Voice ® in WeB DeVelopmenT Beginning PHP and MySQL From Novice to Professional THIRD EDITION cyan maGenTa yelloW Black panTone 123 c W. Jason Gilmore Companion eBook Available www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN-13: 978-1-59059-862-7 ISBN-10: 1-59059-862-8 9 781590 598627 5 4 6 9 9 Learn how to build dynamic, database-driven Web sites using two of the world’s most popular open source technologies. Introduces PHP 5 and PHP 6! Introduces PHP 5 and PHP 6! Beginning THE APRESS ROADMAP The Definitive Guide to MySQL, Third Edition Beginning PHP and MySQL, Third Edition Pro MySQL Pro PHP PHP Objects, Patterns, and Practice, Second Edition Beginning Joomla! Practical Web 2.0 Applications with PHP Visit the companion site at http://www.beginningphpandmysql.com/ Beginning PHP and MySQL From Novice to Professional, Third Edition ■■■ W. Jason Gilmore Gilmore_862-8FRONT.fm Page i Wednesday, February 27, 2008 9:00 AM Beginning PHP and MySQL: From Novice to Professional, Third Edition Copyright © 2008 by W. Jason Gilmore All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-862-7 ISBN-10 (pbk): 1-59059-862-8 ISBN-13 (electronic): 978-1-4302-0299-8 ISBN-10 (electronic): 1-4302-0299-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jonathan Gennick Technical Reviewers: Jay Pipes and Matt Wade Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Senior Project Manager: Tracy Brown Collins Copy Editor: Bill McManus Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Susan Glinert Proofreader: Erin Poe Indexer: John Collin Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http:// www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. Gilmore_862-8FRONT.fm Page ii Wednesday, February 27, 2008 9:00 AM This one is dedicated to you, Ruby! Gilmore_862-8FRONT.fm Page iii Wednesday, February 27, 2008 9:00 AM iv Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii ■ CHAPTER 1 Introducing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Configuring Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ■ CHAPTER 3 PHP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ■ CHAPTER 4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 ■ CHAPTER 5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 ■ CHAPTER 6 Object-Oriented PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 ■ CHAPTER 7 Advanced OOP Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 ■ CHAPTER 8 Error and Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 ■ CHAPTER 9 Strings and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 ■ CHAPTER 10 Working with the File and Operating System . . . . . . . . . . . . . . . . . 277 ■ CHAPTER 11 PEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 ■ CHAPTER 12 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 ■ CHAPTER 13 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 ■ CHAPTER 14 Authenticating Your Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 ■ CHAPTER 15 Handling File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 ■ CHAPTER 16 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 ■ CHAPTER 17 PHP and LDAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 ■ CHAPTER 18 Session Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 ■ CHAPTER 19 Templating with Smarty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 ■ CHAPTER 20 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 ■ CHAPTER 21 Secure PHP Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Gilmore_862-8FRONT.fm Page iv Wednesday, February 27, 2008 9:00 AM v ■ CHAPTER 22 SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 ■ CHAPTER 23 Building Web Sites for the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 ■ CHAPTER 24 MVC and the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601 ■ CHAPTER 25 Introducing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621 ■ CHAPTER 26 Installing and Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 ■ CHAPTER 27 The Many MySQL Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 ■ CHAPTER 28 MySQL Storage Engines and Datatypes . . . . . . . . . . . . . . . . . . . . . . 693 ■ CHAPTER 29 Securing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731 ■ CHAPTER 30 Using PHP with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 ■ CHAPTER 31 Introducing PDO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793 ■ CHAPTER 32 Stored Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819 ■ CHAPTER 33 MySQL Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 ■ CHAPTER 34 MySQL Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 ■ CHAPTER 35 Practical Database Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 ■ CHAPTER 36 Indexes and Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907 ■ CHAPTER 37 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925 ■ CHAPTER 38 Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957 Gilmore_862-8FRONT.fm Page v Wednesday, February 27, 2008 9:00 AM Gilmore_862-8FRONT.fm Page vi Wednesday, February 27, 2008 9:00 AM vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii ■ CHAPTER 1 Introducing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 PHP 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 PHP 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 General Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Practicality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Possibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 ■ CHAPTER 2 Configuring Your Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Installation Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Downloading Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Downloading PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Obtaining the Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Installing Apache and PHP on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Installing Apache and PHP on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Installing IIS and PHP on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Installing IIS and PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Configuring FastCGI to Manage PHP Processes . . . . . . . . . . . . . . . . 22 Testing Your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Configuring PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Configuring PHP at Build Time on Linux . . . . . . . . . . . . . . . . . . . . . . . 25 Customizing the Windows Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Contents Gilmore_862-8FRONT.fm Page vii Wednesday, February 27, 2008 9:00 AM viii ■ CONTENTS Run-Time Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Managing PHP’s Configuration Directives . . . . . . . . . . . . . . . . . . . . . 27 PHP’s Configuration Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Choosing a Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Adobe Dreamweaver CS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Notepad++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 PDT (PHP Development Tools) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Zend Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Choosing a Web Hosting Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Seven Questions for Any Prospective Hosting Provider . . . . . . . . . . 51 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ■ CHAPTER 3 PHP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Embedding PHP Code in Your Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . 56 Default Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Short-Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 ASP Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Embedding Multiple Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Commenting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Single-Line C++ Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Shell Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Multiple-Line C Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Outputting Data to the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The print() Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The printf() Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The sprintf() Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 PHP’s Supported Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Scalar Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Compound Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Converting Between Datatypes Using Type Casting . . . . . . . . . . . . . 69 Adapting Datatypes with Type Juggling . . . . . . . . . . . . . . . . . . . . . . . 70 Type-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Type Identifier Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 PHP’s Superglobal Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Variable Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Gilmore_862-8FRONT.fm Page viii Wednesday, February 27, 2008 9:00 AM [...]... Magazine, and TechTarget He’s the author of several books, including the best-selling Beginning PHP and MySQL: From Novice to Professional, Beginning PHP and PostgreSQL 8: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional Away from the laptop you’ll find Jason starting more home-remodeling projects than he could possibly complete, tickling the ivories, playing chess, and. .. 645 Starting and Stopping MySQL 646 Controlling the Daemon Manually 646 Starting and Stopping MySQL Automatically 648 Configuring and Optimizing MySQL 652 The mysqld_safe Wrapper 652 MySQL s Configuration and Optimization Parameters ... 26 Installing and Configuring MySQL 635 Downloading MySQL 636 Downloading MySQL for Windows 636 Installing MySQL 636 Installing MySQL on Linux 637 Installing and Configuring MySQL on Windows 642 Setting the MySQL Administrator... the most effective use of MySQL He has given sessions on performance tuning at the MySQL Users Conference, RedHat Summit, NY PHP Conference, php| tek, OSCON, and Ohio LinuxFest, among others He lives in Columbus, Ohio, with his wife, Julie, and his four animals In his abundant free time, when not being pestered by his two needy cats and two noisy dogs, he daydreams in PHP code and ponders the ramifications... said, if you’re interested in gaining practical and comprehensive insight into the PHP programming language and MySQL database server, and how these prominent technologies can be used together to create dynamic, database-driven Web applications, this book is for you The feverish work of the respective PHP and MySQL communities prompted this new edition, and with it considerable changes over the previous... developer, and system administrator He currently works for a large financial firm by day and freelances by night He has experience programming in several languages, though he most commonly utilizes PHP and C On the database side of things, he regularly uses MySQL and Microsoft SQL Server As an accomplished system administrator, he regularly has to maintain Windows servers and Linux boxes and prefers... 211 ■CHAPTER 8 Error and Exception Handling 213 Configuration Directives 214 Error Logging 217 Exception Handling 221 Why Exception Handling Is Handy 221 PHP s Exception-Handling Implementation ... best understand the concepts presented within For your convenience, a zip file containing all of the examples can be downloaded from http://www.apress.com Contact Me! I love reader e-mail, and invite you to contact me with comments, suggestions, and questions Feel free to e-mail me at jason@wjgilmore.com Also be sure to regularly check http://www.beginningphpandmysql.com for errata, code, and other updates... alive and kicking, its current incarnation being what you hold in your hands Suffice it to say this project has exceeded my wildest expectations, and I thank Gary and Apress (now a much larger but still ambitious publisher) profusely for the opportunity I’d also like to thank my project manager Tracy Brown Collins for her infinite patience and organizational talents Technical reviewers Jay Pipes and. .. 30 Using PHP with MySQL 767 Handling Installation Prerequisites 768 Enabling the mysqli Extension on Linux/Unix 769 Enabling the mysqli Extension on Windows 769 Managing User Privileges 769 Working with Sample Data 769 Using the mysqli Extension . Introduces PHP 5 and PHP 6! Introduces PHP 5 and PHP 6! Beginning THE APRESS ROADMAP The Definitive Guide to MySQL, Third Edition Beginning PHP and MySQL, . Beginning PHP and Oracle: From Novice to Professional US $46.99 Shelve in PHP/ Web Development User level: Beginner–Intermediate Gilmore THIRD EDITION PHP and MySQL