PHP and MySQL this print for content only—size & color not accurate 7.5 x 9.25 spine = x.xxx" 824 page count Gilmore FOURTH EDITION Beginning PHP and MySQL From Novice to Professional FOURTH EDITION CYAN MAGENTA YELLOW BLACK PANTONE 123 C W. Jason Gilmore Companion eBook Available Learn how to build dynamic, database-driven web sites using two of the world’s most popular open source technologies BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® W. Jason Gilmore, Author of Beginning PHP and PostgreSQL8, Beginning PHP and Oracle US $49.99 Shelve in: Web Development/PHP Programming User level: Beginning/Intermediate THE APRESS ROADMAP Pro PHP Refactoring Pro PHP and jQuery Pro PHP: Patterns, Frameworks, Testing, and More PHP Object-Oriented Solutions Practical Web 2.0 Applications with PHP PHP Objects, Patterns, and Practice, Third Edition PHP for Absolute Beginners Beginning PHP and MySQL, Fourth Edition www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-3114-1 9 781430 231141 5 49 9 9 Beginning Beginning PHP and MySQL: From Novice to Professional Dear Reader, PHP and MySQL have long ranked among the world's most popular technologies for building powerful web sites—and for good reason. Both offer developers an amazing array of features, yet have managed to remain very approachable even among the most novice developer. In the latest edition of this bestselling book, you'll learn all about PHP and MySQL, setting you along the path towards creat- ing the web site you've long dreamed about. I begin with a vast overview of PHP's capabilities, starting with in-depth instructions of the installation and configuration process. Next, I devote several chapters to fundamental PHP concepts, including variables, data types, func- tions, and object orientation. I also introduce key concepts such as HTML form processing, session handling, and AJAX. This new edition also introduces you to several of PHP's latest features, including namespaces, enhanced input filtering and validation, and improved configuration options. 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 data types, administration utilities, and security fea- tures 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 com- municate with MySQL, and you’ll learn how to create and execute queries, perform searches, and carry out other key database tasks from within your web site. Whether you're a newcomer searching for a thorough introduction to these popular technologies or a seasoned developer in need of a comprehensive refer- ence, this book is for you. W. Jason Gilmore THE EXPERT’S VOICE ® IN WEB DEVELOPMENT www.it-ebooks.info www.it-ebooks.info Beginning PHP and MySQL From Novice to Professional Fourth Edition W. Jason Gilmore www.it-ebooks.info Beginning PHP and MySQL: From Novice to Professional, Fourth Edition Copyright © 2010 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-4302-3114-1 ISBN-13 (electronic): 978-1-4302-3115-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Michelle Lowman Development Editor: Tom Welsh Technical Reviewer: Matt Wade Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editors: Anne Collett and Jennifer L. Blackwell Copy Editor: Mary Behr Compositor: Bytheway Publishing Services Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 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 www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit 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 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(s) 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 www.apress.com. www.it-ebooks.info CONTENTS AT A GLANCE iii Contents at a Glance About the Author xxix About the Technical Reviewer xxx Acknowledgments xxxi Introduction xxxii Chapter 1: Introducing PHP 1 Chapter 2: Configuring Your Environment 9 Chapter 3: PHP Basics 41 Chapter 4: Functions 91 Chapter 5: Arrays 103 Chapter 6: Object-Oriented PHP 135 Chapter 7: Advanced OOP Features 159 Chapter 8: Error and Exception Handling 175 Chapter 9: Strings and Regular Expressions 191 Chapter 10: Working with the File and Operating System 229 Chapter 11: PEAR 257 Chapter 12: Date and Time 269 Chapter 13: Working with HTML Forms 287 Chapter 14: Authenticating Your Users 303 Chapter 15: Handling File Uploads 319 Chapter 16: Networking 331 Chapter 17: PHP and LDAP 351 www.it-ebooks.info CONTENTS AT A GLANCE iv Chapter 18: Session Handlers 367 Chapter 19: Templating with Smarty 387 Chapter 20: Web Services 411 Chapter 21: Securing Your Web Site 427 Chapter 22: Creating Ajax-enhanced Features with jQuery and PHP 437 Chapter 23: Building Web Sites for the World 449 Chapter 24: Introducing the Zend Framework 457 Chapter 25: Introducing MySQL 477 Chapter 26: Installing and Configuring MySQL 487 Chapter 27: The Many MySQL Clients 505 Chapter 28: MySQL Storage Engines and Data Types 527 Chapter 29: Securing MySQL 557 Chapter 30: Using PHP with MySQL 587 Chapter 31: Introducing PDO 607 Chapter 32: Stored Routines 627 Chapter 33: MySQL Triggers 649 Chapter 34: MySQL Views 659 Chapter 35: Practical Database Queries 673 Chapter 36: Indexes and Searching 693 Chapter 37: Transactions 709 Chapter 38: Importing and Exporting Data 719 Index 733 www.it-ebooks.info CONTENTS v Contents About the Author xxix About the Technical Reviewer xxx Acknowledgments xxxi Introduction xxxii Chapter 1: Introducing PHP 1 History 1 PHP 4 2 PHP 5 3 PHP 5.3 4 PHP 6 5 General Language Features 5 Practicality 5 Power 6 Possibility 7 Price 7 Summary 8 Chapter 2: Configuring Your Environment 9 Installation Prerequisites 9 Downloading Apache 10 Downloading PHP 10 Downloading the Documentation 11 Installing Apache and PHP on Linux 12 www.it-ebooks.info CONTENTS vi Installing Apache and PHP on Windows 13 Installing IIS and PHP on Windows 15 Testing Your Installation 16 Configuring PHP 18 Configuring PHP at Build Time on Linux 18 Customizing the Windows Build 19 Run-Time Configuration 19 Managing PHP’s Configuration Directives 19 PHP’s Configuration Directives 21 Choosing a Code Editor 37 Adobe Dreamweaver CS5 37 Notepad++ 37 PDT (PHP Development Tools) 38 Zend Studio 38 Choosing a Web Hosting Provider 38 Seven Questions for Any Prospective Hosting Provider 39 Summary 41 Chapter 3: PHP Basics 41 Embedding PHP Code in Your Web Pages 41 Default Syntax 42 Short-Tags 42 Script 43 ASP Style 43 Embedding Multiple Code Blocks 44 Commenting Your Code 44 Single-Line C++ Syntax 44 Shell Syntax 44 Multiple-Line C Syntax 45 www.it-ebooks.info CONTENTS vii Outputting Data to the Browser 45 The print() Statement 46 The echo() Statement 47 The printf() Statement 47 The sprintf() Statement 49 PHP’s Supported Data Types 49 Scalar Data Types 49 Compound Data Types 51 Converting Between Data Types Using Type Casting 52 Adapting Data Types with Type Juggling 53 Type-Related Functions 55 Type Identifier Functions 56 Identifiers 56 Variables 57 Variable Declaration 58 Variable Scope 59 PHP’s Superglobal Variables 62 Variable Variables 68 Constants 68 Expressions 69 Operands 69 Operators 69 String Interpolation 76 Double Quotes 77 Escape Sequences 77 Single Quotes 78 Curly Braces 79 Heredoc 79 www.it-ebooks.info CONTENTS viii Nowdoc 80 Control Structures 80 Conditional Statements 80 Looping Statements 82 File-Inclusion Statements 88 Summary 91 Chapter 4: Functions 91 Invoking a Function 91 Creating a Function 92 Passing Arguments by Value 93 Passing Arguments by Reference 94 Default Argument Values 94 Using Type Hinting 95 Returning Values from a Function 96 Recursive Functions 97 Function Libraries 100 Summary 101 Chapter 5: Arrays 103 What Is an Array? 103 Creating an Array 105 Creating Arrays with array() 105 Extracting Arrays with list() 106 Populating Arrays with a Predefined Value Range 107 Testing for an Array 107 Outputting an Array 108 Printing Arrays for Testing Purposes 109 Adding and Removing Array Elements 110 Adding a Value to the Front of an Array 110 www.it-ebooks.info [...]... Setting the MySQL Administrator Password 495 Starting and Stopping MySQL 495 Controlling the Daemon Manually 495 Configuring and Optimizing MySQL .496 The mysqld_safe Wrapper 497 MySQL s Configuration and Optimization Parameters 497 The my.cnf File 501 Configuring PHP to Work with MySQL 504 Reconfiguring PHP on Linux... 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 version In addition to updating the material to reflect features found in the latest PHP and MySQL releases, you'll find a new chapter introducing the concept of AJAX and the... carefully revised, and in some cases heavily modified, in order to both update and improve upon the previous edition' s material If you're new to PHP, I recommend beginning with Chapter 1, because it's likely that first gaining fundamental knowledge will be of considerable benefit when reading later chapters If you know PHP but are new to MySQL, consider beginning with Chapter 25 Intermediate and advanced... 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 to deal with FreeBSD... Variables 374 Encoding and Decoding Session Data 375 Practical Session-Handling Examples 377 Automatically Logging In Returning Users 378 Generating a Recently Viewed Document Index 379 Creating Custom Session Handlers .381 Tying Custom Session Functions into PHP s Logic 382 Using Custom MySQL- Based Session Handlers 382 Summary... (www.wjgilmore.com), a consulting, publishing, and training company with experience serving clientele ranging from local legal firms to Fortune 500 companies He has been teaching developers about web development for over a decade, having written six books, including the bestselling "Beginning PHP and MySQL, Third Edition, " "Easy PHP Websites with the Zend Framework," and "Easy PayPal with PHP, " published more than 100... Should You Use Stored Routines? 627 Stored Routine Advantages 627 Stored Routine Disadvantages 628 How MySQL Implements Stored Routines .629 xxiv www.it-ebooks.info CONTENTS Creating a Stored Routine 629 Declaring and Setting Variables 632 Executing a Stored Routine 633 Creating and Using Multistatement Stored Routines... always my goal to write with practicality in mind, providing instruction that you can apply to your own situation Given the size of this book, it's probably apparent that I also tried to squeeze out every last drop of such practicality from the subject matter That said, if you're interested in gaining practical and comprehensive insight into the PHP programming language and MySQL database, and how these... to jump around as necessary; after all, this isn't a romance novel Regardless of your reading strategy, I've attempted to compartamentalize the material found in each chapter so you can quickly learn each topic without having to necessarily master other chapters beyond those that concentrate on the technology fundamentals Furthermore, novices and seasoned PHP/ MySQL developers alike have something to. .. Chapter 25: Introducing MySQL 477 What Makes MySQL So Popular? 477 Flexibility 477 Power 478 Flexible Licensing Options 480 A (Hyper)Active User Community 481 The Evolution of MySQL 481 MySQL 4 481 MySQL 5 482 MySQL 5.1 483 MySQL 5.4 and 5.5 . Beginning PHP and MySQL From Novice to Professional Fourth Edition W. Jason Gilmore www.it-ebooks.info Beginning PHP and MySQL: . 978-1-4302-3114-1 9 781430 231141 5 49 9 9 Beginning Beginning PHP and MySQL: From Novice to Professional Dear Reader, PHP and MySQL have long ranked among the