CYAN BLACK BOOKS FOR PROFESSIONALS BY PROFESSIONALS Companion W . - Tlu

Transcription

CYANMAGENTAYELLOWBLACKPANTONE 123 Cwww.it-ebooks.infoBOOKS FOR PROFESSIONALS BY PROFESSIONALS W. Jason Gilmore, Author ofBeginning PHP andPostgreSQL8, Beginning PHPand OracleDear Reader,PHP and MySQL have long ranked among the world's most popular technologiesfor building powerful web sites—and for good reason. Both offer developers anamazing array of features, yet have managed to remain very approachable evenamong 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 creating the web site you've long dreamed about.I begin with a vast overview of PHP's capabilities, starting with in-depthinstructions of the installation and configuration process. Next, I devote severalchapters to fundamental PHP concepts, including variables, data types, functions, and object orientation. I also introduce key concepts such as HTML formprocessing, session handling, and AJAX. This new edition also introduces you toseveral of PHP's latest features, including namespaces, enhanced input filteringand validation, and improved configuration options.I then introduce many of MySQL's key features, beginning by guiding youthrough MySQL's installation and configuration process. Next, you'll learn aboutMySQL's storage engines and data types, administration utilities, and security features. I'll also introduce you to several advanced features such as triggers, storedprocedures, and views. Along the way, you'll gain insight into PHP's ability to communicate with MySQL, and you’ll learn how to create and execute queries, performsearches, and carry out other key database tasks from within your web site.Whether you're a newcomer searching for a thorough introduction to thesepopular technologies or a seasoned developer in need of a comprehensive reference, this book is for you.W. Jason GilmoreCompanion eBookTHE APRESS ROADMAPPHPfor Absolute BeginnersSee last page for detailson 10 eBook versionBeginningPHP and MySQL,Fourth EditionPHP Objects,Patterns, and Practice,Third EditionPro PHP:Patterns, Frameworks,Testing, and MorePHP Object-OrientedSolutionsProPHP RefactoringPracticalWeb 2.0 Applicationswith PHPProPHP and jQuerywww.apress.comISBN 978-1-4302-3114-15 49 9 9US 49.99BeginningFOURTHEDITIONGilmoreSOURCE CODE ONLINECompanioneBook AvailablePHP and MySQLBeginning PHP and MySQL:From Novice to ProfessionalTHE EXPERT’S VOICE IN WEB DEVELOPMENTBeginningPHP andMySQLFrom Novice to ProfessionalLearn how to build dynamic, database-driven web sites usingtwo of the world’s most popular open source technologiesFOURTH EDITIONW. Jason GilmoreShelve in:Web Development/PHPProgrammingUser level:Beginning/Intermediate9 781430 231141this print for content only—size & color not accurate7.5 x 9.25 spine x.xxx" 824 page count

www.it-ebooks.info

www.it-ebooks.infoBeginning PHPand MySQLFrom Novice to ProfessionalFourth Edition W. Jason Gilmore

www.it-ebooks.infoBeginning PHP and MySQL: From Novice to Professional, Fourth EditionCopyright 2010 by W. Jason GilmoreAll 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 retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-4302-3114-1ISBN-13 (electronic): 978-1-4302-3115-8Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbolwith every occurrence of a trademarked name, logo, or image we use the names, logos, and images onlyin an editorial fashion and to the benefit of the trademark owner, with no intention of infringement ofthe trademark.The use in this publication of trade names, trademarks, service marks, and similar terms, even if they arenot identified as such, is not to be taken as an expression of opinion as to whether or not they are subjectto proprietary rights.President and Publisher: Paul ManningLead Editor: Michelle LowmanDevelopment Editor: Tom WelshTechnical Reviewer: Matt WadeEditorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, JonathanGennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper,Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, TomWelshCoordinating Editors: Anne Collett and Jennifer L. BlackwellCopy Editor: Mary BehrCompositor: Bytheway Publishing ServicesIndexer: BIM Indexing & Proofreading ServicesArtist: April MilneCover Designer: Anna IshchenkoDistributed 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@springersbm.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 ourSpecial 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 everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shall haveany liability to any person or entity with respect to any loss or damage caused or alleged to be causeddirectly 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 GLANCEContents 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.351iii

www.it-ebooks.info CONTENTS AT A GLANCE 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 .733iv

www.it-ebooks.info CONTENTSContents About the Author. xxix About the Technical Reviewer . xxx Acknowledgments . xxxi Introduction . xxxii Chapter 1: Introducing PHP .1History .1PHP 4 . 2PHP 5 . 3PHP 5.3 . 4PHP 6 . 5General Language Features.5Practicality. 5Power . 6Possibility . 7Price . 7Summary .8 Chapter 2: Configuring Your Environment .9Installation Prerequisites .9Downloading Apache. 10Downloading PHP . 10Downloading the Documentation . 11Installing Apache and PHP on Linux .12v

www.it-ebooks.info CONTENTSInstalling Apache and PHP on Windows .13Installing IIS and PHP on Windows .15Testing Your Installation .16Configuring PHP.18Configuring PHP at Build Time on Linux . 18Customizing the Windows Build . 19Run-Time Configuration.19Managing PHP’s Configuration Directives . 19PHP’s Configuration Directives . 21Choosing a Code Editor.37Adobe Dreamweaver CS5. 37Notepad . 37PDT (PHP Development Tools) . 38Zend Studio . 38Choosing a Web Hosting Provider.38Seven Questions for Any Prospective Hosting Provider . 39Summary .41 Chapter 3: PHP Basics .41Embedding PHP Code in Your Web Pages .41Default Syntax . 42Short-Tags. 42Script . 43ASP Style . 43Embedding Multiple Code Blocks . 44Commenting Your Code .44Single-Line C Syntax . 44Shell Syntax. 44Multiple-Line C Syntax . 45vi

www.it-ebooks.info CONTENTSOutputting Data to the Browser .45The print() Statement. 46The echo() Statement . 47The printf() Statement . 47The sprintf() Statement. 49PHP’s Supported Data Types .49Scalar Data Types. 49Compound Data Types. 51Converting Between Data Types Using Type Casting . 52Adapting Data Types with Type Juggling . 53Type-Related Functions. 55Type Identifier Functions . 56Identifiers.56Variables .57Variable Declaration . 58Variable Scope. 59PHP’s Superglobal Variables . 62Variable Variables. 68Constants.68Expressions .69Operands . 69Operators. 69String Interpolation .76Double Quotes . 77Escape Sequences . 77Single Quotes . 78Curly Braces . 79Heredoc . 79vii

www.it-ebooks.info CONTENTSNowdoc. 80Control Structures.80Conditional Statements . 80Looping Statements. 82File-Inclusion Statements. 88Summary .91 Chapter 4: Functions.91Invoking a Function .91Creating a Function.92Passing Arguments by Value . 93Passing Arguments by Reference. 94Default Argument Values. 94Using Type Hinting. 95Returning Values from a Function . 96Recursive Functions . 97Function Libraries.100Summary .101 Chapter 5: Arrays.103What Is an Array? .103Creating an Array.105Creating Arrays with array() . 105Extracting Arrays with list() . 106Populating Arrays with a Predefined Value Range . 107Testing for an Array . 107Outputting an Array.108Printing Arrays for Testing Purposes. 109Adding and Removing Array Elements .110Adding a Value to the Front of an Array. 110viii

www.it-ebooks.info CONTENTSAdding a Value to the End of an Array . 110Removing a Value from the Front of an Array . 111Removing a Value from the End of an Array. 111Locating Array Elements.111Searching an Array . 111Retrieving Array Keys . 113Retrieving Array Values . 113Traversing Arrays.113Retrieving the Current Array Key . 114Retrieving the Current Array Value . 114Retrieving the Current Array Key and Value . 115Moving the Array Pointer . 115Passing Array Values to a Function . 116Determining Array Size and Uniqueness .117Determining the Size of an Array. 117Counting Array Value Frequency . 118Determining Unique Array Values. 119Sorting Arrays.119Reversing Array Element Order . 119Flipping Array Keys and Values . 121Sorting an Array. 121Merging, Slicing, Splicing, and Dissecting Arrays .126Merging Arrays . 127Recursively Appending Arrays. 127Combining Two Arrays . 128Slicing an Array . 128Splicing an Array . 129Calculating an Array Intersection . 131Calculating Associative Array Intersections . 131ix

www.it-ebooks.info CONTENTSCalculating Array Differences . . 132Calculating Associative Array Differences. 133Other Useful Array Functions.133Returning a Random Set of Keys. .

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 .