PHP is a scripting language for writing Web pages containing embedded code that is executed whenever a page is accessed and that can generate dynamic content to be included as part of the output sent to a client's Web browser. This chapter describes how to write PHP-based Web applications that use MySQL. For a comparison of PHP with the C and Perl DBI APIs for MySQL programming, see Chapter 5, "Introduction to MySQL Programming."
The examples in this chapter draw on our sampdb sample database, using the tables created for the grade-keeping project and for the Historical League in Chapter 1, "Getting Started with MySQL and SQL." The applications described here should run under either PHP 3 or PHP 4, although I recommend PHP 4 for performance and security reasons.
This chapter was written under the assumption that you'll use PHP in conjunction with the Apache Web server. In addition, PHP must be built with the MySQL C client library linked in, or it will not know how to access MySQL databases. If you need to obtain any of this software, see Appendix A, "Obtaining and Installing Software." That appendix also provides instructions for obtaining the sampdb distribution that contains the example scripts developed in this chapter. You can download the scripts to avoid typing them in yourself. You'll find the scripts pertaining to the chapter under the phpapi directory of that distribution.
Under UNIX, PHP can be used as an Apache module or as a standalone interpreter used as a traditional CGI program. Under Windows, PHP can run only as a standalone program unless you use Apache 2.x and PHP 4. In that case, you have the option of running PHP as an Apache module. On either platform, running PHP as a module is preferable for performance reasons.
For the most part, this chapter describes only those MySQL-related PHP functions that are needed for the discussion here. For a more comprehensive listing of PHP's MySQL functions, see Appendix H, "PHP API Reference." You'll likely also want to consult the PHP manual, which describes all the functions that PHP provides, including those for using databases other than MySQL (PHP is not limited to working with MySQL any more than DBI is). The manual is available from the PHP Web site at http://www.php.net/. Another helpful source of information is PHP Functions Essential Reference by Greant et al. (New Riders, 2002).
Filenames for PHP scripts generally end with an extension that allows your Web server to recognize that they should be executed by invoking the PHP interpreter. If you use an extension that isn't recognized, your PHP scripts will be served as plain text. The extension used in this chapter is .php. Another common extension is .phtml. For instructions on configuring Apache to recognize the extension you want to use, see Appendix A. (If you are not in control of the Apache installation on your machine, check with the system administrator to find out the proper extension to use.) The appendix also describes how to set up Apache to treat any script named index.php as the default page for the directory in which it is located, similar to the way Apache uses files named index.html.
To use the scripts developed in this chapter, you'll need to install them where your Web server can access them. I'll adopt the convention in this chapter that the U.S. Historical League and grade-keeping projects have their own directories called ushl and gp at the top level of the Apache document tree. To set up your Web server that way, you should create those directories now. If the Web site host is www.snake.net, pages in those two directories will have URLs that begin as follows:
http://www.snake.net/ushl/... http://www.snake.net/gp/...
For example, the home pages in each directory can be called index.php and are accessed as follows:
http://www.snake.net/ushl/index.php
http://www.snake.net/gp/index.php
If you have Apache configured to use index.php as the default page for a directory, the following URLs are equivalent to the preceding ones:
http://www.snake.net/ushl/
http://www.snake.net/gp/