only for RuBoard - do not distribute or recompile Copyright Table of Contents Index Full Description Reviews Reader reviews Errata Web Database Applications with PHP & MySQL Hugh E. Williams David Lane Publisher: O'Reilly First Edition March 2002 ISBN: 0-596-00041-3, 582 pages Web Database Applications with PHP and MySQL offers web developers a mixture of theoretical and practical information on creating web database applications. Using PHP and MySQL, two open source technologies that are often combined to develop web applications, the book offers detailed information on designing relational databases and on web application architecture, both of which will be useful to readers who have never dealt with these issues before. The book also introduces Hugh and Dave's Online Wine Store, a complete (but fictional) online retail site implemented using PHP and MySQL. only for RuBoard - do not distribute or recompile This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile Web Database Applications with PHP & MySQL Preface What This Book Is About What You Need to Know How This Book Is Organized How to Use This Book Conventions Used in This Book How to Contact Us Web Site and Code Examples Acknowledgments 1. Database Applications and the Web 1.1 Three-Tier Architectures 1.2 The Client Tier 1.3 The Middle Tier 1.4 The Database Tier 1.5 Our Case Study 2. PHP 2.1 Introducing PHP 2.2 Conditions and Branches 2.3 Loops 2.4 A Working Example 2.5 Arrays 2.6 Strings 2.7 Regular Expressions 2.8 Date and Time Functions 2.9 Integer and Float Functions 2.10 User-Defined Functions 2.11 Objects This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. 2.12 Common Mistakes 3. MySQL and SQL 3.1 Database Basics 3.2 Quick Start Guide 3.3 MySQL Command Interpreter 3.4 Managing Databases, Tables, and Indexes 3.5 Inserting, Updating, and Deleting Data 3.6 Querying with SQL SELECT 3.7 Join Queries 3.8 Modifying the Database 3.9 Functions 3.10 More on SQL and MySQL 4. Querying Web Databases 4.1 Connecting to a MySQL Database 4.2 Formatting Results 4.3 Case Study: The Front-Page Panel 4.4 Interacting with Other DBMSs Using PHP 5. User-Driven Querying 5.1 User Input 5.2 Querying with User Input 5.3 Case Study: Previous and Next Browsing 5.4 Case Study: Producing a select List 6. Writing to Web Databases 6.1 Database Inserts, Updates, and Deletes 6.2 Issues in Writing Data to Databases 7. Validation on the Server and Client 7.1 Validation and Error Reporting for Web Database Applications 7.2 Server-Side Validation 7.3 Client-Side Validation with JavaScript 8. Sessions 8.1 Building Applications That Keep State 8.2 Session Management Over the Web 8.3 PHP Session Management 8.4 Case Study: Adding Sessions to the Winestore 8.5 When to Use Sessions 9. Authentication and Security 9.1 HTTP Authentication 9.2 HTTP Authentication with PHP 9.3 Authentication Using a Database This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. 9.4 Web Database Applications and Authentication 9.5 Protecting Data on the Web 10. Winestore Customer Management 10.1 Overview of the Winestore Application 10.2 Customer Management 10.3 Authenticating Users 10.4 The Winestore Include Files 11. The Winestore Shopping Cart 11.1 The Winestore Home Page 11.2 The Shopping Cart Architecture 11.3 Managing Redirection 12. Ordering and Shipping at the Winestore 12.1 Finalizing Orders 12.2 HTML and Email Receipts 13. Related Topics 13.1 Automated Housekeeping 13.2 Templates 13.3 Searching and Browsing A. Installation Guide A.1 Installing MySQL, Apache, and PHP A.2 Installing the Winestore Examples A.3 Installing Apache to Use SSL A.4 Installation Resources B. Internet and Web Protocols B.1 The Internet B.2 Hypertext Transfer Protocol C. Modeling and Designing Relational Databases C.1 The Relational Model C.2 Entity-Relationship Modeling D. Managing Sessions in the Database Tier D.1 Using a Database to Keep State D.2 PHP Session Management D.3 MySQL Session Store E. Resources E.1 Client Tier Resources E.2 Middle Tier Resources E.3 Database Tier Resources This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. E.4 Security and Cryptography Resources Colophon only for RuBoard - do not distribute or recompile This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile Web Database Applications with PHP & MySQL Copyright © 2002 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, 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. The O'Reilly logo is a registered trademark of O'Reilly & Associates, 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 & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The association between the image of a platypus and the topic of web database applications with PHP and MySQL is a trademark of O'Reilly & Associates, Inc. While every precaution has been taken in the preparation of this book, the publisher and the author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile Preface Web database applications integrate databases and the Web. Well-known web destinations such as online auction sites, retail stores, news sites, discussion forums, and personalized home pages are all examples of web database applications. The popularity of these applications stems from their accessibility and usability: thousands of users can access the same data at the same time without the need to install additional software on their machines. only for RuBoard - do not distribute or recompile This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile What This Book Is About This book is for developers who want to build database applications that are integrated with the Web. It presents the principles and techniques of developing small- to medium-scale web database applications that store, manage, and retrieve data, as well as the basic techniques for securing an application. 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 from users. An important feature of this book is our ongoing case study, Hugh and Dave's Online Wines. It's a complete but fictional online retail store that allows users to browse and search a database of wines, add items to a shopping cart, manage their membership, and purchase wines. Searching, browsing, storing user data, validating user input, managing user transactions, and security are each the subject of a chapter, and each topic is illustrated with examples from the case study. The completed winestore scripts are presented and briefly discussed at the end of the book. We use open source software. Our database management system (DBMS) 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 libraries that interact with more than 15 relational database systems, the web environment, and many other services. We use PHP to develop the application logic that brings together the Web and the relational database management system (RDBMS). Apache is our web server of choice. only for RuBoard - do not distribute or recompile This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. only for RuBoard - do not distribute or recompile What You Need to Know This book is about understanding and developing application logic that brings databases and the Web together. We introduce database systems over the course of the book, but our discussions don't replace a book or class dedicated to relational database theory, or a book about a specific relational database system such as MySQL. Likewise, we assume you are already familiar with the Web. We introduce but don't delve deeply into the three key web protocols, HTML, HTTP, and TCP/IP. We also assume you can program in a third-generation programming language such as C, C++, Java, Perl, FORTRAN, or Visual Basic. Our introduction to the PHP web scripting language doesn't assume you are familiar with web scripting or are an expert programmer, but we do assume you understand the basic HTML constructs and are familiar with the popular web browsers. If you can author an HTML document with a text editor that contains a <form> and a <table> element, you have sufficient HTML skills to use this book. It is the principles of structure in the markup process that are important, not the attractiveness or usability of the presentation in the web browser. We introduce advanced HTML concepts as required, but an HTML guide such as O'Reilly's HTML and XHTML: The Definitive Guide, by Chuck Musciano and William Kennedy, is a useful resource for understanding and building web database applications. You may also find O'Reilly's Programming PHP, by Rasmus Lerdorf and Kevin Tatroe useful as well. You don't need a detailed understanding of relational databases to use this book, but a working knowledge is helpful. We present the relational database theory needed for developing simple applications, and we cover many other basic concepts, including how to tell when a database is the method of choice to store data, the architecture of a DBMS, the database query language SQL, and a case study that models system requirements and converts the model to a database design. This book isn't a substitute for the many good resources on database theory, however, it's enough to begin developing the underlying databases for many web database applications. We briefly introduce web servers and networking in Chapter 1 and provide additional material in Appendix B. Both web servers and networking are important to a web database application but aren't the focus of this book. We present enough information to set up a web server and to understand how it fits in the architecture of a web database application. For many applications, this is sufficient. Likewise, we present sufficient detail so that you will understand what networking and network protocol issues impact web database This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [...]... or recompile Chapter 1 Database Applications and the Web With the growth of the Web over the past decade, there has been a similar growth in services that are accessible over the Web Many new services are web sites that are driven from data stored in databases Examples of web database applications include news services that provide access to large data repositories, e-commerce applications such as online... well for structuring and presenting information using a web browser application TCP/IP is an effective networking protocol that transfers data between applications over the Internet and has little impact on web database application developers The problem in building web database applications is interfacing traditional database applications to the Web using HTTP This is where the complex application logic... This book is about bringing together the Web and databases Most web database applications do this through three layers of application logic At the base is a database management system (DBMS) and a database At the top is the client web browser used as an interface to the application Between the two lies most of the application logic, usually developed with a web server-side scripting language that can... presents for web database applications The HTTP protocol is used by web browsers to request resources from web servers, and for web servers to return responses (A longer introduction to the underlying web protocols—including more examples of HTTP requests and responses—can be found in Appendix B.) HTTP allows resources to be communicated and shared over the Web From a network perspective, HTTP is an applications- layer... to web database applications because of its integration tools for the Web and database environments In particular, the flexibility of embedding scripts in HTML pages permits easy integration with the client tier The database- tier integration support is also excellent, with more than 15 libraries available to interact with almost all popular database management systems [3] From the Security Space web. .. communicates data between the other tiers On top is the client tier , usually web browser software that interacts with the application Figure 1-1 The three-tier architecture model of a web database application The formality of describing most web database applications as three-tier architectures hides the reality that the applications must bring together different protocols and software The majority... this book Chapter 1 to Chapter 3 introduce web database applications, PHP, MySQL, and SQL: Chapter 1 Discusses the three-tier architecture commonly used in web database applications and in those that we discuss in this book We introduce each of the three tiers and the features of each, and we introduce the software tools that we use We also briefly introduce web protocols The chapter concludes with an... products Database applications have been around for over 30 years, and many have been deployed using network technology long before the Web existed The point-of-service systems used by bank tellers are obvious examples of early networked database applications Terminals are installed in bank branches, and access to the bank's central database application is provided through a wide area network These early applications. .. formulates responses In the case of web database applications, these requests are often for programs that interact with an underlying database management system The web server we use throughout this book is the Apache Software Foundation's Apache HTTP server, the open source web server used by more than 60% of Internet connected computers.[2] [2] From The Netcraft Web Server Survey, http://www.netcraft.com/survey/... register it T only for RuBoard - do not distribute or recompile Web Site and Code Examples Code examples from this book, data used to create the online winestore database, and the completed winestore application can be found at this book's web site, http://www.oreilly.com/catalog/webdbapps/ or at the authors' web site, http://www.webdatabasebook.com only for RuBoard - do not distribute or recompile . pages Web Database Applications with PHP and MySQL offers web developers a mixture of theoretical and practical information on creating web database applications. . do not distribute or recompile Preface Web database applications integrate databases and the Web. Well-known web destinations such as online auction sites,