【正文】
ct to a MySQL server fromPHP, how to use PHP to access and retrieve stored data, and how to correctly display information to the user.1 The Process The basic steps of performing a query, whether using the mysql mandline tool or PHP, are the same:? Connect to the database.? Select the database to use.? Build a SELECT statement.? Perform the query.? Display the results. We’ll walk through each of these steps for both plain PHP and PEAR functions.2 Resources When connecting to a MySQL database, you will use two new resources. The first is the link identifier that holds all of the information necessary to connect to the database for an active connection. The other resource is the results resource. It contains all information required to retrieve results from an active database query’s result set. You’ll be creating and assigning both resources in this chapter.3 Querying the Database with PHP Functions In this section, we introduce how to connect to a MySQL database with PHP. It’s quite simple, and we’ll begin shortly with examples, but we should talk briefly about what actually happens. When you try connecting to a MySQL database, the MySQL server authenticates you based on your username and password. PHP handles connecting to the database for you, and it allows you to start performing queries and gathering data immediately.As in Chapter 8, we’ll need the same pieces of information to connect to the database:? The IP address of the database server? The name of the database? The username? The passwordBefore moving on, make sure you can log into your database using the MySQL mandline client.4 Including Database Login Details You’re going to create a file to hold the information for logging into MySQL. Storing this information in a file you include is remended. If you change the database password, there is only one place that you need to change it, regardless of how many PHP files you have that access the don’t have to worry about anyone directly viewing the file and getting your database login details. The file, if requested by itself, is processed as a PHP file and returns a blank page.You’ll need to restart Apache, and then MySQL support will be enabled.6 Selecting the Database Now that you’re connected, the next step is to select which database to use with the mysql_select_db mand. It takes two parameters: the database name an