Andrew Curioso, Ronald Bradford, Patrick Galbraith Wrox Programmer to Programmer™ Join the discussion @ p2p.wrox.com PHP and MySQL ® Expert Curioso et al. $44.99 USA $53.99 CAN Programming Languages / PHP wrox.com Programmer Forums Join our Programmer to Programmer forums to ask and answer programming questions about this book, join discussions on the hottest topics in the industry, and connect with fellow programmers from around the world. Code Downloads Take advantage of free code samples from this book, as well as code samples from hundreds of other books, all ready to use. Read More Find articles, ebooks, sample chapters, and tables of contents for hundreds of books, and more reference resources on programming topics that matter to you. As the world’s most popular, general purpose, open source scripting language, PHP is frequently used with MySQL to create high-traffic, mission-critical applications. This indispensable book shares proven, author-tested best practices and expert techniques that can be applied to even the most demanding MySQL-driven PHP apps. You’ll explore ways to extend MySQL with user-defined functions, write PHP extensions, and solve problems when PHP and MySQL alone are not enough and other techniques or third-party tools are necessary. Expert PHP and MySQL: • Reviews essential techniques, such as design patterns, complex queries, and advanced regular expression • Addresses advanced PHP concepts, such as iterators and closures • Demonstrates using Gearman for multitasking in your web applications • Discusses caching using memcached with MySQL and your PHP web applications • Discusses ways to create PHP Extensions and MySQL User Defined Functions • Shows how to use Sphinx for search functionality in your PHP web applications Andrew Curioso is a senior software engineer at MyVBO (Virtual Business Office) creating an innovative and scalable infrastructure for Rich Internet Applications using PHP and MySQL. Ronald Bradford is a technology strategist and advisor specializing in MySQL, Drizzle and related web technologies. With a primary focus on architecture, design, performance analysis and scalability of web applications, his consulting portfolio ranges from Internet startups to Fortune 500 companies. Patrick Galbraith is a principal software engineer at Lycos, adding new PHP and MySQL offerings for Tripod Users, switching remaining apps using Oracle to MySQL as well as other interesting projects. Wrox Expert books present the wisdom accumulated by an experienced author team who is recognized as experts by the programming community. These experts challenge professional developers to examine their current practices in pursuit of better results. Best practices and expert techniques for today’s most demanding apps Expert PHP and MySQL ® www.it-ebooks.info Related Wrox Books Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 PHP, Apache, and MySQL are the three key open source technologies that form the basis for most active web servers. This book guides you through the entire process of setting up your own site and walks you through every step, from the installation of PHP, Apache, and MySQL to database management, security, and integration with other technologies. The multi-platform approach addresses installation and usage on both Linux® and Windows®, and two common-themed, reusable web sites are examined. Upon completion of this book, you’ll be able to create well designed, dynamic web sites using open source tools. Beginning PHP 5.3 ISBN: 978-0-470-41396-8 As one of the most popular open-source web-programming languages in use today, PHP is an ideal server-side scripting language that connects HTML-based web pages to a backend database for dynamic content. It allows you to create anything from a simple form-to-email script to a web forum application, a blogging platform, or a content management system. This guide introduces the PHP language and shows you how to write powerful web applications using PHP. Professional PHP 5 ISBN: 978-0-7645-7282-1 This book has a pragmatic focus on how to use PHP in the larger scheme of enterprise-class software development. It covers UML modeling and presents objects and object hierarchies that, when completed, comprise a robust toolkit that developers will be able to reuse on future projects. This book is designed to arm you with the sort of constructs that are available out of the box with platforms such as Java and .NET — from simple utility classes like Collection and Iterator, to more complex constructs like Model/View/Controller architectures and state machines. Professional PHP Design Patterns ISBN: 978-0-470-49670-1 This book bridges the gap between the PHP and the older programming language by applying those tried and tested Design Patterns to native PHP applications. It starts with an introduction to Design Patterns, describes their use and importance, and details where you’ve seen them already. The book continues through a host of Design Patterns with code examples and explanations. Finally, an in-depth case study shows you how to plan your next application using Design Patterns, how to program those patterns in PHP, and how to revise and refactor an existing block of code using Design Patterns from the book. The author approaches the concepts in technical yet usable formats that are familiar to both programmers who are self-taught and those with more extensive formal education. Get more out of wrox.com Programmer to Programmer ™ Interact Take an active role online by participating in our P2P forums @ p2p.wrox.com 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! 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 www.it-ebooks.info CONTENTS AT A GLANCE INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix CHAPTER 1 Techniques Every Expert Programmer Needs to Know . . . . . . . . . . . . . . .1 CHAPTER 2 Advanced PHP Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 CHAPTER 3 MySQL Drivers and Storage Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 CHAPTER 4 Improving Performance through Caching . . . . . . . . . . . . . . . . . . . . . . . . . 131 CHAPTER 5 memcached and MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 CHAPTER 6 Advanced MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 CHAPTER 7 Extending MySQL with User-Defi ned Functions . . . . . . . . . . . . . . . . . . 271 CHAPTER 8 Writing PHP Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 CHAPTER 9 Full-Text Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 CHAPTER 10 Multi-tasking in PHP and MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 CHAPTER 11 Rewrite Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 CHAPTER 12 User Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 CHAPTER 13 Understanding the INFORMATION_SCHEMA . . . . . . . . . . . . . . . . . . . . 477 CHAPTER 14 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 CHAPTER 15 Command-Line and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517 CHAPTER 16 Optimization and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 563120ffirs.indd i563120ffirs.indd i 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info 563120ffirs.indd ii563120ffirs.indd ii 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info EXPERT PHP and MySQL® 563120ffirs.indd iii563120ffirs.indd iii 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info 563120ffirs.indd iv563120ffirs.indd iv 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info EXPERT PHP and MySQL® Andrew Curioso Ronald Bradford Patrick Galbraith 563120ffirs.indd v 563120ffirs.indd v 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info Expert PHP and MySQL® Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-56312-0 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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 specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or pro- motional 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 pub- lisher 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. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2010920658 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 affi liates, in the United States and other coun- tries, and may not be used without written permission. MySQL is a registered trademark of MySQL AB. All other trade- marks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. 563120ffirs.indd vi563120ffirs.indd vi 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info This book is dedicated to my wife, Laura. Without your support this book wouldn’t be possible. Thank you for being by my side and planning the wedding while I was doing my writing! For MySQL Culture, past, present, and future. To many in the MySQL community: you are more than colleagues, you are great friends. This book is dedicated to my son, Kiran Patrick. “Kiran” means “ray of light” in Sanskrit, and you are a ray of light in my life! 563120ffirs.indd vii 563120ffirs.indd vii 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info 563120ffirs.indd viii563120ffirs.indd viii 2/18/10 9:07:32 AM2/18/10 9:07:32 AM www.it-ebooks.info [...]... applications that need to store and access large amounts of data or requires lots of processing ➤ Programmers who just find things like design patterns, PHP extensions, and MySQL UDFs interesting and who want to learn more This book assumes that you also have a working knowledge of C as well and that you have at least compiled a C programmer or two (PHP and MySQL count) PHP extensions and MySQL User Defi ned Functions... essentials and touches on some advanced MySQL topics ➤ Chapters 7 and 8 dive into extending PHP and MySQL with C They are the only chapters in the book where you’re not writing code in just PHP or SQL ➤ Chapter 9 goes over full-text search and introduces Sphinx ➤ Chapter 10 covers multitasking and discusses Gearman ➤ Chapters 11 and 12 cover essentials including advanced rewrite rules, custom session handling... tools that you need to write mission critical applications with PHP and MySQL — including high-traffic ones The hope is that you can use some of the skills covered in this book to create your own open source projects and eventually contribute to the PHP and MySQL communities Or perhaps you just use this book to help you create the next PHP and MySQL powered success story WHO THIS BOOK IS FOR This book is... Returning Values Built-In Functions Creating and Consuming PHP API Functions More Notes and Creating Helper Functions Input/Output 323 324 326 328 329 329 331 334 334 335 336 339 340 Standard Out Files and Streams Networking Errors and Warnings 340 341 344 346 Arrays and Hash Tables 347 Building and Accessing Arrays Accessing and Modifying Hash Tables Objects and Interfaces 347 349 349 Creating a Class... an advanced PHP, MySQL, and C programmer but you’ll probably want to keep some other Wrox books handy for reference WHAT THIS BOOK COVERS PHP and MySQL are both well established with many libraries and extensions available It would be impossible to cover every expert topic in a single book Likewise, this book doesn’t try to cover beginner level topics at all The authors did, however, hand pick topics... 435 438 439 439 441 442 442 CHAPTER 11: REWRITE RULES Using Rewrite Rules 443 444 Understanding the Purpose and Structure of Rewrite Rules Understanding and Controlling Rewrite Rule Flow Conditional Rules Logging and Optimization Rewrite Maps 444 445 447 450 451 Built-In Maps Random and Text Lookups Using PHP and MySQL Summary 451 452 452 456 CHAPTER 12: USER AUTHENTICATION Designing the Database HTTP-Based... CHAPTER 1: TECHNIQUES EVERY EXPERT PROGRAMMER NEEDS TO KNOW Object-Oriented PHP 1 2 Instantiation and Polymorphism Interfaces Magic Methods and Constants Design Patterns 2 7 8 11 Using MySQL Joins 20 INNER JOIN OUTER JOIN Other JOIN Syntax Complex Joins 22 24 26 27 MySQL Unions GROUP BY in MySQL Queries WITH ROLLUP HAVING 28 30 31 32 Logical Operations and Flow Control in MySQL Logic Operators Flow... extending PHP and MySQL using C In PHP we cover extension writing In MySQL we cover User Defi ned Functions The book focuses on critical skills and best practices in the areas of security, optimization, and software architecture The examples are written with the assumption that you already know the basics so common code is occasionally stripped out to make the examples more readable and focused And as... xxv 2/18/10 9:07:48 AM CONTENTS Delta Indexes Merging Indexes 389 394 Developing Applications That Use SPHINX Sphinx and PHP 395 395 Summary 399 CHAPTER 10: MULTI-TASKING IN PHP AND MYSQL Gearman What Is Gearman? 40 1 401 402 Installing and Running Gearman Using the Gearman MySQL UDFs PHP and Gearman Narada: A Search Engine Application Obtaining Narada Narada Components Database Tables for the Search... session handling and user security ➤ Chapter 13 goes over MySQL INFORMATION_SCHEMA ➤ Chapter 14 touches on security some more ➤ Chapters 15 and 16 wrap up the book with other uses of PHP and MySQL besides web applications and optimizing and debugging xxx www.it-ebooks.info 563120flast.indd xxx 2/18/10 9:20:19 AM INTRODUCTION WHAT YOU NEED TO USE THIS BOOK Apart from a willingness to learn and all the skills . PHP extensions, and solve problems when PHP and MySQL alone are not enough and other techniques or third-party tools are necessary. Expert PHP and MySQL: . practices and expert techniques for today’s most demanding apps Expert PHP and MySQL ® www.it-ebooks.info Related Wrox Books Beginning PHP5 , Apache, and MySQL