In the previous chapter, we looked at the basics of the PHP language and how it can interact with a MySQL database. In this chapter, we explore how to access and exchange information with a MySQL server from within a practical PHP application. As a running example, we present a simple wedding gift registry application that allows wedding guests to log in, view a list of gifts wanted by the bride and groom, and reserve gifts that they plan to purchase by putting them on a shopping list.
In the process, we show how the common database functions are used in practice, including how to:
Call PHP library functions to connect to the MySQL DBMS and handle MySQL errors with PHP
Manage DBMS credentials with include files
Execute queries through the DBMS connection, and retrieve query result sets
Present query results using HTML
Create HTML form environments using PHP
Interact with the user and preprocess user data to minimize security risks
Add session support to an application so that a user can log in and log out
Pass data between scripts by creating embedded hypertext links in HTML
Use HTTP headers
The database and script files for this example are available from the book’s web site; we recommend you download these and look at them while reading through this chapter. Even better, set up the application on your system and try it out. The program source code is also listed in the Appendix.