PHP and MySQL Web Development
Fourth Edition
Luke Welling
Laura Thomson
Addison-Wesley
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Cape Town Sydney Tokyo Singapore Mexico City
Contents at a Glance
Table of Contents
Introduction
WELCOME TO PHP AND MYSQL WEB DEVELOPMENT. Within its pages, you will find distilled knowledge from our experiences using PHP and MySQL, two of the hottest web development tools around.
In this introduction, we cover
Lets get started.
Why You Should Read This Book
This book will teach you how to create interactive websites from the simplest order form through to complex, secure e-commerce sites or interactive Web 2.0 sites. Whats more, youll learn how to do it using open source technologies.
This book is aimed at readers who already know at least the basics of HTML and have done some programming in a modern programming language before but have not necessarily programmed for the Internet or used a relational database. If you are a beginning programmer, you should still find this book useful, but digesting it might take a little longer. Weve tried not to leave out any basic concepts, but we do cover them at speed. The typical readers of this book want to master PHP and MySQL for the purpose of building a large or commercial website. You might already be working in another web development language; if so, this book should get you up to speed quickly.
We wrote the first edition of this book because we were tired of finding PHP books that were basically function references. These books are useful, but they dont help when your boss or client has said, Go build me a shopping cart. In this book, we have done our best to make every example useful. You can use many of the code samples directly in your website, and you can use many others with only minor modifications.
What You Will Learn from This Book
Reading this book will enable you to build real-world, dynamic websites. If youve built websites using plain HTML, you realize the limitations of this approach. Static content from a pure HTML website is just thatstatic. It stays the same unless you physically update it. Your users cant interact with the site in any meaningful fashion.
Using a language such as PHP and a database such as MySQL allows you to make your sites dynamic: to have them be customizable and contain real-time information.
We have deliberately focused this book on real-world applications, even in the introductory chapters. We begin by looking at a simple online ordering system and work our way through the various parts of PHP and MySQL.
We then discuss aspects of electronic commerce and security as they relate to building a real-world website and show you how to implement these aspects in PHP and MySQL.
In the final part of this book, we describe how to approach real-world projects and take you through the design, planning, and building of the following projects:
User authentication and personalization
Shopping carts
Web-based email
Mailing list managers
Web forums
PDF document generation
Web services with XML and SOAP
Web 2.0 application with Ajax
You should be able to use any of these projects as is, or you can modify them to suit your needs. We chose them because we believe they represent some the most common web-based applications built by programmers. If your needs are different, this book should help you along the way to achieving your goals.
What Is PHP?
PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be executed each time the page is visited. Your PHP code is interpreted at the web server and generates HTML or other output that the visitor will see.
PHP was conceived in 1994 and was originally the work of one man, Rasmus Lerdorf. It was adopted by other talented people and has gone through four major rewrites to bring us the broad, mature product we see today. As of November 2007, it was installed on more than 21 million domains worldwide, and this number is growing rapidly. You can see the current number at http://www.php.net/usage.php .
PHP is an Open Source project, which means you have access to the source code and can use, alter, and redistribute it all without charge.
PHP originally stood for Personal Home Page but was changed in line with the GNU recursive naming convention (GNU = Gnus Not Unix) and now stands for PHP Hypertext Preprocessor.
The current major version of PHP is 5. This version saw a complete rewrite of the underlying Zend engine and some major improvements to the language.
The home page for PHP is available at http://www.php.net .
The home page for Zend Technologies is http://www.zend.com .
What Is MySQL?
MySQL (pronounced My-Ess-Que-Ell) is a very fast, robust, relational database management system (RDBMS). A database enables you to efficiently store, search, sort, and retrieve data. The MySQL server controls access to your data to ensure that multiple users can work with it concurrently, to provide fast access to it, and to ensure that only authorized users can obtain access. Hence, MySQL is a multiuser, multithreaded server. It uses Structured Query Language (SQL), the standard database query language. MySQL has been publicly available since 1996 but has a development history going back to 1979. It is the worlds most popular open source database and has won the Linux Journal Readers Choice Award on a number of occasions.
MySQL is available under a dual licensing scheme. You can use it under an open source license (the GPL) free as long as you are willing to meet the terms of that license. If you want to distribute a non-GPL application including MySQL, you can buy a commercial license instead.
Why Use PHP and MySQL?
When setting out to build a website, you could use many different products.
You need to choose the following:
Hardware for the web server
An operating system
Web server software
A database management system