Learning PHP and MySQL By Michele Davis, Jon Phillips Publisher: O'Reilly Pub Date: June 2006 Print ISBN-10: 0-596-10110-4 Print ISBN-13: 978-0-59-610110-7 Pages: 376 Table of Contents | Index The PHP scripting language and MySQL open-source database are quite effective independently, but together they make a simply unbeatable team When working hand-in-hand, they serve as the standard for the rapid development of dynamic, database-driven websites This combination is so popular, in fact, that it's attracting many programming newbies who come from a web or graphic design background and whose first language is HTML If you fall into this ever-expanding category, then this book is for you Learning PHP and MySQL starts with the very basics of the PHP language, including strings and arrays, pattern matching and a detailed discussion of the variances in different PHP versions Next, it explains how to work with MySQL, covering information on SQL data access for language and data fundamentals like tables and statements Finally, after it's sure that you've mastered these separate concepts, the book shows you how to put them together to generate dynamic content In the process, you'll also learn about error handling, security, HTTP authentication and more If you're a hobbyist who is intimidated by thick, complex computer books, then this guide definitely belongs on your shelf Learning PHP and MySQL explains everything from basic concepts to the nuts and bolts of performing specific tasks in plain English Part of O'Reilly's bestselling "Learning" series, the book is an easy-to-use resource designed specifically for newcomers It's also a launching pad for future learning, providing you with a solid foundation for more advanced development Learning PHP and MySQL By Michele Davis, Jon Phillips Publisher: O'Reilly Pub Date: June 2006 Print ISBN-10: 0-596-10110-4 Print ISBN-13: 978-0-59-610110-7 Pages: 376 Table of Contents | Index Copyright Preface Chapter 1 Dynamic Content and the Web Section 1.1 PHP and MySQL's Place in Web Development Section 1.2 The Components of a PHP Application Section 1.3 Integrating Many Sources of Information Section 1.4 Requesting Data from a Web Page Section 1.5 Chapter 1 Questions Chapter 2 Installation Section 2.1 Developing Locally Section 2.2 Working Remotely Section 2.3 Chapter 2 Questions Chapter 3 Exploring PHP Section 3.1 PHP and HTML Text Section 3.2 Coding Building Blocks Section 3.3 Chapter 3 Questions Chapter 4 PHP Decision Making Section 4.1 Expressions Section 4.2 Operator Concepts Section 4.3 Conditionals Section 4.4 Looping Section 4.5 Chapter 4 Questions Chapter 5 Functions Section 5.1 Calling Functions Section 5.2 Defining Functions Section 5.3 Object-Oriented Programming Section 5.4 Chapter 5 Questions Chapter 6 Arrays Section 6.1 Array Fundamentals Section 6.2 Chapter 6 Questions Chapter 7 Database Basics Section 7.1 Database Design Section 7.2 Structured Query Language Section 7.3 Chapter 7 Questions Chapter 8 Using MySQL Section 8.1 MySQL Database Section 8.2 Managing the Database Section 8.3 Backing Up and Restoring Data Section 8.4 Advanced SQL Section 8.5 Chapter 8 Questions Chapter 9 Getting PHP to Talk to MySQL Section 9.1 The Process Section 9.2 Querying the Database with PHP Functions Section 9.3 Using PEAR Section 9.4 Chapter 9 Questions Chapter 10 Working with Forms Section 10.1 Building a Form Section 10.2 Templates Section 10.3 Chapter 10 Questions Chapter 11 Practical PHP Section 11.1 String Functions Section 11.2 Date and Time Functions Section 11.3 File Manipulation Section 11.4 Calling System Calls Section 11.5 Chapter 11 Questions Chapter 12 Modifying MySQL Objects and PHP Data Section 12.1 Changing Database Objects from PHP Section 12.2 Manipulating Table Data Section 12.3 Displaying Results with Embedded Links Section 12.4 Presenting a Form to Add and Process in One File Section 12.5 Updating Data Section 12.6 Deleting Data Section 12.7 Performing a Subquery Section 12.8 Chapter 12 Questions Chapter 13 Cookies, Sessions, and Access Control Section 13.1 Cookies Section 13.2 PHP and HTTP Authentication Section 13.3 Sessions Section 13.4 Using Auth_HTTP to Authenticate Section 13.5 Chapter 13 Questions Chapter 14 Security Section 14.1 Section 14.2 Session Security Section 14.3 Chapter 14 Questions Chapter 15 Validation and Error Handling Section 15.1 Validating User Input with JavaScript Section 15.2 Pattern Matching Section 15.3 Redisplaying a Form After PHP Validation Fails Section 15.4 Chapter 15 Questions Chapter 16 Sample Applications Section 16.1 Building a Blog Section 16.2 Chapter 16 Questions Chapter 17 Finishing Your Journey Section 17.1 Finding Help on the Web Section 17.2 Web Sites Section 17.3 Zend Section 17.4 Lampshade Section 17.5 Chapter 17 Questions Answers to Chapter Questions Section A.1 Chapter 1 Section A.2 Chapter 2 Section A.3 Chapter 3 Section A.4 Chapter 4 Section A.5 Chapter 5 Section A.6 Chapter 6 Section A.7 Chapter 7 Section A.8 Chapter 8 Section A.9 Chapter 9 Section A.10 Chapter 10 Section A.11 Chapter 11 Section A.12 Chapter 12 Section A.13 Chapter 13 Section A.14 Chapter 14 Section A.15 Chapter 15 Section A.16 Chapter 16 Section A.17 Chapter 17 Colophon About the Authors Index Learning PHP and MySQL by Michele E Davis and Jon A Phillips Copyright © 2006 O'Reilly Media, Inc All rights reserved Printed in the United States of America Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O'Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Editor: Simon St.Laurent Production Editor: Mary Brady Copyeditor: Linley Dolby Proofreader: Mary Brady Indexer: Johnna Van Hoose Dinse Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read Printing History: June 2006: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc Learning PHP and MySQL, the image of Kookaburra birds, and related trade dress are trademarks of O'Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O'Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 0-596-10110-4 [M] Preface PHP and MySQL are a powerful combination that together make it easy to create web applications If you've been creating web pages but want to build more sophisticated sites that can grow and interact with users, PHP and MySQL let you build your foundations quickly, and subsequently build on them to meet your needs Our goal is to help you learn the ins and outs of PHP and MySQL and save you some of the "Why doesn't that work?" moments that we've already been through We'll show you what to watch for and how to fix these issues without pulling out your hair Audience This book is for people who want to know how to create dynamic web sites That could include graphic designers who are already working in an IT firm or advertising firm creating static web sites, who may need to move forward with coding database-driven web sites It might also include people who already know, say, Flash development and HTML markup, but need to expand their repertoire of skills to database and open source programming environments Assumptions This Book Makes This book assumes you understand how web browsers work and have a basic understanding of HTML Some understanding of JavaScript may be useful (for Chapter 15) but isn't generally required You might also be over-qualified If you already know how to create pages using MySQL and PHP, then you'd probably be better off with a book that is more a reference than a Learning book, such as one from the Nutshell series Organization of This Book This book starts out with an overview of how all of the pieces you'll be working with fit together Because there are multiple languages and technologies that interact to form the dynamic web pages, it's best to start with a solid foundation of how the pieces work together The PHP that you'll learn works as a sort of a total integration package for dynamic web sites Next, we'll walk through installing the core software packages on your local computer This book focuses on PHP and MySQL, but making this work also usually requires the Apache web server The PHP interpreter works with the web server when processing dynamic content Finally, you'll install the MySQL database Installation is covered on the PC, Mac, and Linux systems You can also use a hosted ISP account to develop your pages, if you don't want to install everything locally Since PHP plays an important role in pulling everything together, we take the time to explain the basics of working with the PHP language This includes language essentials such as data types, program flow logic, and variables Functions, arrays, and forms each get their own chapter to fully explore them Since you may be new to databases in general, we ease into MySQL by first explaining concepts that apply to designing and using any relational database Then we give specific examples of using MySQL to interact with your data Once you can get data in and out of the database, you'll need to work with PHP to integrate that data into your dynamic content Security and access control to your web pages each get their own chapter While security may sound like a dull subject, it's still a huge issue if you store any information that's private on special characters templates variables global scope static super global types values PHP tags phpMyAdmin precedence, operators precision, strings predefined constants, PHP preg_match primary keys printf function Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] quantifiers queries databases, selecting displaying executing fetching forms and login details PEAR PHP functions results, displaying SQL aliases ORDER BY keyword SELECT WHERE clause SQL injection steps for performing subqueries Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] radio buttons records, databases reference parameters references arrays variables regular expressions relational databases foreign keys primary keys relationships many-to-many one-to-many one-to-one relational operators remote login, installation and renaming tables require function require_once function resources further reading web chat forums listservs user groups web sites restoring data Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] scalar values, arrays scope, variables classes PHP scripting, cross-site scripting attacks searches strings Second Normal Form, database normalization security administrative pages authentication, HTTP cross-site scripting attacks databases access separate users global variables, automatic hosting, sharing include files magic quotes sessions fixation hijacking user data, trust SELECT command SELECT query selecting databases server-side processing, PHP session identifiers session variables, serialization session_start function sessions ending fixation garbage collection hijacking starting timeout setting cookies sh1 (secure hash algorithm) Smarty, installation sorting arrays special characters, PHP strings sprintf function SQL (Structured Query Language) 2nd LEFT JOIN ON clause queries aliases ORDER BY keyword SELECT WHERE clause searches SELECT command tables adding data creating WHERE clause, logical operators SQL injection statements continue else else if if include include_once require require_once switch static methods static variables PHP string functions checking for strings databases calculating length concatenation location padding strings position search and replace trimming strings upper-, lowercase extracting portion padding and precision and printf sprintf strlen strtolower strtoupper strings length PHP combining comparing special characters regular expressions strlen function strstr function strtolower function strtoupper function structured information subqueries super global variables, PHP switch statement system calls Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] tables, databases adding data column data type columns adding deleting creating 2nd data, adding deleting dropping errors joining renaming templates PHP Smarty installation template engine templating ternary operators ? (question mark) operator testing for functions text checkboxes hidden elements radio buttons selects text areas text boxes text areas text boxes Third Normal Form, database normalization threading, Apache timeouts, sessions timestamps touch command trimming strings Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] uinque identifiers, generating unary operators Unix, PEAR installation unlink function updates, data uploading files uppercase strings URL wrappers user data, trust user groups usernames, storage users databases, separate input validation, JavaScript and Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] validating data validation dates failed, redisplaying form files pattern matching user input, JavaScript and values arrays adding looping through referencing forms, accessing in variables, PHP variables arrays, extracting from global, automatic PHP global scope static super global variables types values references scope, classes static Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] web pages dynamic static web resources web server web sites WHERE clause logical operators and while loops Windows, PEAR installation Index [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [Z] Zend ... together wasn't an afterthought when the developers created the PHP and MySQL interfaces PHP and MySQL have open source power As they are both open source projects, PHP and MySQL can both be used for free MySQL client libraries are no longer... doesn't sound like much fun, and neither does learning a whole new complicated language Well, thankfully, PHP and MySQL make creating dynamic web sites simpler, easier, and faster 1.1 PHP and MySQL' s Place in Web... Advantages of Using PHP with MySQL There are several factors that make using PHP and MySQL together a natural choice: PHP and MySQL work well together PHP and MySQL have been developed with each other in mind, so they are easy to use together