Accessing the MySQL DBMS with PHP

This section shows you how to connect to and query the MySQL DBMS using PHP. The examples are illustrated with the first of three scripts from a simple but useful PHP application: a wedding gift registry that allows 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. The complete application—including the database, scripts, and instructions on how to install it on your web site—is available at http://www.webdatabasebook.com/wedding/.

The gift registry application illustrates the basics of interacting with MySQL using PHP. It shows how to:

  • Call PHP library functions to connect to the MySQL DBMS, execute queries through the DBMS connection, and retrieve query result sets

  • Present query results using HTML

  • 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 out

  • Pass data between scripts by creating embedded hypertext links in HTML, develop HTML <form> environments, and use HTTP headers

  • Handle MySQL errors with PHP

  • Manage DBMS credentials with include files

This section introduces the basics of interacting with MySQL using PHP. The script described here displays the gifts that are unreserved and the gifts that have been reserved by the current user. The output of the script rendered by a Netscape browser is shown in Figure 11-1. The script assumes the guest has already logged in. The script for logging in is discussed later in Section 11.7. The script that adds and removes gifts from the guest’s shopping list is discussed in Section 11.6.

The wedding gift registry
Figure 11-1. The wedding gift registry

The scripts in the gift registry use only the common PHP MySQL library functions. Chapter 18 is a reference to PHP’s complete MySQL function library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset