• • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic Web Database Application with PHP and MySQL, 2nd Edition By David Lane, Hugh E Williams Publisher : O'Reilly Pub Date : May 2004 ISBN : 0-596-00543-1 Pages : 816 This new edition has been redesigned around the rich offerings of PEAR Several of these, including the Template package and the database-independent query API, are fully integrated into examples and thoroughly described in the text In addition, through a complex sample application Hugh and Dave's Wine Store all the important techniques of dynamic content are introduced Good design is emphasized, such as dividing logic from presentation The book introduces PHP 5 and MySQL 4.1 features, while providing techniques that can be used on older versions of the software that are still in widespread use • • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic Web Database Application with PHP and MySQL, 2nd Edition By David Lane, Hugh E Williams Publisher : O'Reilly Pub Date : May 2004 ISBN : 0-596-00543-1 Pages : 816 Copyright Preface What This Book Is About What You Need to Know How This Book Is Organized Conventions Used in This Book How to Contact Us Acknowledgments How to Use This Book Using Code Examples Web Site and Code Examples Chapter 1 Database Applications and the Web Section 1.1 The Web Section 1.2 Three-Tier Architectures Chapter 2 The PHP Scripting Language Section 2.1 Introducing PHP Section 2.2 Conditions and Branches Section 2.3 Loops Section 2.5 Working with Types Section 2.7 A Working Example Section 2.4 Functions Section 2.6 User-Defined Functions Chapter 3 Arrays, Strings, and Advanced Data Manipulation in PHP Section 3.1 Arrays Section 3.2 Strings Section 3.3 Regular Expressions Section 3.5 Integers and Floats Section 3.4 Dates and Times Chapter 4 Introduction to Object-Oriented Programming with PHP 5 Section 4.1 Classes and Objects Section 4.2 Inheritance Section 4.3 Throwing and Catching Exceptions Chapter 5 SQL and MySQL Section 5.1 Database Basics Section 5.2 MySQL Command Interpreter Section 5.3 Managing Databases and Tables Section 5.5 Querying with SQL SELECT Section 5.7 Case Study: Adding a New Wine Section 5.4 Inserting, Updating, and Deleting Data Section 5.6 Join Queries Chapter 6 Querying Web Databases Section 6.1 Querying a MySQL Database Using PHP Section 6.2 Processing User Input Section 6.3 MySQL Function Reference Chapter 7 PEAR Section 7.1 Overview Section 7.2 Core Components Section 7.3 Packages Chapter 8 Writing to Web Databases Section 8.1 Database Inserts, Updates, and Deletes Section 8.2 Issues in Writing Data to Databases Chapter 9 Validation with PHP and JavaScript Section 9.1 Validation and Error Reporting Principles Section 9.3 JavaScript and Client-Side Validation Section 9.2 Server-Side Validation with PHP Chapter 10 Sessions Section 10.1 Introducing Session Management Section 10.2 PHP Session Management Section 10.3 Case Study: Using Sessions in Validation Section 10.5 PHP Session API and Configuration Section 10.4 When to Use Sessions Chapter 11 Authentication and Security Section 11.1 HTTP Authentication Section 11.2 HTTP Authentication with PHP Section 11.3 Form-Based Authentication Section 11.4 Protecting Data on the Web Chapter 12 Errors, Debugging, and Deployment Section 12.1 Errors Section 12.2 Common Programming Errors Section 12.3 Custom Error Handlers Chapter 13 Reporting Section 13.1 Creating a Report Section 13.2 Producing PDF Section 13.3 PDF-PHP Reference Chapter 14 Advanced Features of Object-Oriented Programming in PHP 5 Section 14.1 Working with Class Hierarchies Section 14.2 Class Type Hints Section 14.3 Abstract Classes and Interfaces Section 14.4 Freight Calculator Example Chapter 15 Advanced SQL Section 15.1 Exploring with SHOW Section 15.2 Advanced Querying Section 15.3 Manipulating Data and Databases Section 15.5 Automating Querying Section 15.7 Backup and Recovery Section 15.9 Tuning MySQL Section 15.4 Functions Section 15.6 Table Types Section 15.8 Managing Users and Privileges Chapter 16 Hugh and Dave's Online Wines:A Case Study Section 16.1 Functional and System Requirements Section 16.3 Common Components Section 16.2 Application Overview Chapter 17 Managing Customers Section 17.1 Code Overview Section 17.2 Customer Validation Section 17.3 The Customer Form Chapter 18 The Shopping Cart Section 18.1 Code Overview Section 18.2 The Winestore Home Page Section 18.3 The Shopping Cart Implementation Chapter 19 Ordering and Shipping at the Online Winestore Section 19.1 Code Overview Section 19.2 Credit Card and Shipping Instructions Section 19.3 Finalizing Orders Section 19.4 HTML and Email Receipts Chapter 20 Searching and Authentication in the Online Winestore Section 20.1 Code Overview Section 20.2 Searching and Browsing Section 20.3 Authentication Appendix A Linux Installation Guide Section A.1 Finding Out What's Installed Section A.2 Installation Overview Section A.3 Installing MySQL Section A.5 Installing PHP Section A.4 Installing Apache Section A.6 What's Needed for This Book Appendix B Microsoft Windows Installation Guide Section B.1 Installation Overview Section B.2 Installing with EasyPHP Section B.3 What's Needed for This Book Appendix C Mac OS X Installation Guide Section C.1 Getting Started Section C.2 Installing MySQL Section C.3 Setting Up Apache and PHP Section C.4 What's Needed for This Book Appendix D Web Protocols Section D.1 Network Basics Section D.2 Hypertext Transfer Protocol Appendix E Modeling and Designing Relational Databases Section E.1 The Relational Model Section E.2 Entity-Relationship Modeling Appendix F Managing Sessions in theDatabase Tier Section F.1 Using a Database to Keep State Section F.2 PHP Session Management Section F.3 MySQL Session Store Appendix G Resources Section G.1 Client Tier Resources Section G.2 Middle-Tier Resources Section G.3 Database Tier Resources Section G.4 Security and Cryptography Resources Appendix H The Improved MySQL Library Section H.1 New Features Section H.2 Getting Started Section H.3 Using the New Features Colophon Index Copyright © 2004, 2002 O'Reilly Media, Inc Printed in the United States of America Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O'Reilly & Associates books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc Web Database Applications with PHP and MySQL, the image of a platypus, 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 Preface There are lots of PHP and MySQL resources So why did we decide to add this book to the market? We made the decision after we started teaching graduate students how to program with PHP in 1999 We found that the PHP and MySQL manuals, as well as most books, train people to use particular tools But almost no resources explained the principles of programming for the Web We realized that Web administrators and programmers needed to know more than what PHP functions to use and how to write SQL queries That's where this book comes in: it'll help you learn about web database development, as well as understand the principles This book explains what to do and why, along with how it's done in PHP and MySQL You'll find information here that you won't find elsewhere Hopefully, you'll use this knowledge with whatever web tools you choose in the future But you'll also learn about the breadth and depth of PHP and MySQL When you finish this book, you'll be able to build an online store, a portal, or a content management system What This Book Is About This book is for developers who want to build database applications that are integrated with the Web We show you the principles and techniques for developing small- to medium-scale web database applications that store, manage, and retrieve data The architecture we describe is a successful framework for applications that can run on modest hardware and process more than a million hits per day We show you all of the critical tasks you need to know to build successful web software We cover programming fundamentals for the Web We show you the principles and practice of working with databases using the SQL query language We teach you about tracking users with sessions, securing an application, separating presentation from code, writing databaseindependent code, writing reports, adding error handling, and advanced object-oriented and database topics An important feature of this book is our case study, Hugh and Dave's Online Wines It's a complete but fictional online retail store that illustrates how most of the techniques described in the book can be put together to build a real application The winestore application allows users to browse and search a database of wines, add items to a shopping cart, manage their membership, and purchase wines It has all the basic security, user-tracking, and error-handling features of a real-world application It features a medium-size database that we use in querying examples throughout the book We use Open Source software, and we show you how to use it on Unix-based platforms such as Linux and Mac OS X, and under Microsoft Windows 2000, 2003, and XP Our database server is MySQL, a system known for its suitability to applications that require speed but low resource overheads Our scripting language is PHP, which is best known for its function strcasecmp( ) function (PHP) strcmp( ) function (PHP) strftime( ) function (PHP) string expressions string functions (MySQL) string literals strings 2nd arrays, splitting into case, changing characters replacing in translating trimming from comparing formatting length padding regular expressions, matching with [See regular expressions] sprintf( ) and printf( ) functions (PHP) supported conversion types substrings extraction from strings replacing translating substrings, replacing timestamps, generating from date strings trimming whitespace validation of email addresses legal character tests URLs Zip and postcodes wildcard matching strlen( ) function (PHP) strncasecmp( ) function (PHP) strncmp( ) function (PHP) strtolower( ) function (PHP) strtotime( ) function (PHP) 2nd strtoupper( ) function (PHP) 2nd strtr( ) function (PHP) strval( ) function (PHP) substr( ) function (PHP) substr_replace( ) function (PHP) substring( ) function (MySQL) substrings [See strings, substrings] SUM( ) function (MySQL) superglobals switch statement system( ) function (PHP) security risks of [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] tables attributes modifiers checking and repair creating deleting types tainted data tan( ) function (MySQL) TCP/IP (Transmission Control Protocol/Internet Protocol) telephone database creating record creation, HTML form for reload problem and solutions templates/custreceipt.tpl (winestore database) templates/details.tpl (winestore database) templates/email.tpl (winestore database) templates/index.tpl (winestore database) templates/orderreceipt.tpl (winestore database) templates/search.tpl (winestore database) 2nd templates/showcart.tpl (winestore database) 2nd templates/winestore.tpl (winestore database) 2nd text editors thin clients three-tier architecture, web database applications throw statement 2nd 3rd time validation time( ) function (PHP) timestamp( ) function (MySQL) timestamps generating from date strings subsecond timestamp functions TRACE request (HTTP) transactions triggers trigonometric functions trim( ) function (MySQL) trim( ) function (PHP) TRUNCATE statement truncate( ) function (MySQL) try catch statement 2nd two-phase locking two-tier architecture type-casting operators types 2nd gettype( ), print_r( ), and var_dump( ) functions (PHP) type conversion automatic conversion examples variables boolean functions for type checking testing, setting, and unsetting [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] uasort( ) function (PHP) ucfirst( ) function (PHP) ucwords( ) function (PHP) uksort( ) function (PHP) uniform resource identifiers uniform resource locators [See URLs] union clause Unix automated querying PEAR installation and upgrade PEAR package inspection report output formats UNLOCK TABLES statement necessary use of unregisterLogin( ) function (PHP) unrepeatable reads unset( ) function (PHP) 2nd array elements, removing with UPDATE statement joins and update( ) function (JavaScript) updatecart.php (winestore database) 2nd 3rd 4th upper( ) function (MySQL) URLs (uniform resource locators) absolute and relative URLs fragment identifiers host and service identification nonstandard TCP ports parameters and queries protocol resource identification URL encoding validation of user input processing queries using tainted data user-driven querying user variables user( ) function (MySQL) user-defined functions parameter types and return types reusing with include and require files variables passing to functions users table (winestore database) users, creating USING clause usort( ) function (PHP) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] validation client-side versus server-side client-side with JavaScript advantages debugging DOM 2nd events example script examples functions generic validation function JavaScript syntax loops and conditionals methods and properties navigator object objects common approaches effective models error reporting and ineffective models informative error messages post-validation models rekeying of data, avoiding server-side with PHP credit cards dates and times mandatory data numbers PEAR Date package, using strings time validation sessions and var_dump( ) function (PHP) array values, displaying with variable substitution variables 2nd destroying escape sequences and global variables passing to functions assigning by reference by reference default parameter values static variables types checking testing, setting, and unsetting variable assignment variable scope views [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] weak entities 2nd primary keys web browsers web database applications three-tier architecture web databases [See databases] Web protocols HTTP [See HTTP] IP addresses ports TCP/IP web servers 2nd web services PEAR packages week( ) function (MySQL) WHERE clause 2nd whitespace, trimming from strings Windows [See Microsoft Windows] winestore application Linux installation Mac OS X installation Windows installation winestore database adding a wine application overview application templates authentication module 2nd 3rd 4th changing passwords users table browsing and searching module 2nd components connection and query script custom error handler customer management module 2nd 3rd code overview customer form customer receipt page customer validation functions performed customer table database parameters entities ER model of general-purpose functions homepage Linux installation Mac OS X installation modules ordering and shipping module 2nd 3rd code overview confirmation email credit card and shipping instructions email receipt finalizing orders functions performed HTML receipts tables password management PEAR DB connection, query and results PEAR ITX template class, usage in relations in requirements script files and functions scripts searching and browsing module 2nd tables select lists on HTML forms shopping cart module 2nd 3rd adding items code overview emptying the cart functions performed implementation tables updating quantities viewing the cart source code validation Windows installation winestore homepage winestoreFormTemplate class winestoreTemplate class buttons and button parameters winestore.inc (winestore database) 2nd winestore.tpl (winestore database) 2nd WITH ROLLUP modifier write( ) function (PHP) writeln( ) method (JavaScript) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] XML PEAR packages for report output format, using for xpdf [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] Zip code validation Zip programs ... usually includes the title, author, publisher, and ISBN For example: "Web Database Applications with PHP and MySQL, Second Edition, by Hugh E Williams and David Lane Copyright 2004 O'Reilly Media, Inc., 0-596-00543-1."... Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc Web Database Applications with PHP and MySQL, the image of a platypus, and related trade dress are trademarks of O'Reilly... to Chapter 5 introduce web database applications, PHP, MySQL, and SQL: Chapter 1 Discusses the three-tier architecture commonly used in web database applications, and how data is exchanged between browsers and servers