【正文】
br /br /39。pages39。Pages: 39。br / 39。author39。Author: 39。br /39。title39。Title: 39。br /br /39。Pages: 39。br / 39。Author: 39。br /39。Title: 39。 } When the database executes the query, all of the results forma result set. These results correspond to the rows that you saw upon doing a query using the mysql mandline client. To display them, you process each row, one at a time. Fetching and Displaying Use mysql_fetch_row to get the rows from the result set. Its syntax is: array mysql_fetch_row ( resource $result)。 20 The query string could also use a variable in the WHERE clause to limit which rows are returned based on user information or another query. Now that you have your query assigned to a variable, you can execute it. Executing the Query To have the database execute the query, use the mysql_query function. It takes two parameters—the query and, optionally, the database link—and returns the result. Save a link to the results in a variable called, you guessed it, $result! This is also a good place to check the return code from mysql_query to make sure that there were no errors in the query string or the database connection by verifying that $result is not FALSE: // Execute the query $result = mysql_query( $query )。 $query = $select.$column.$from.$tables.$where。 $where = 39。 $tables = 39。 $from = 39。 $column = 39。 } Again, it’s good practice to check for an error and display it every time you access the database. While it’s possible to call mysql_select_db multiple times within the same script, it’s not considered good practice. Now that you’ve got a good database connection, you’re ready to execute your SQL query. Building the SQL SELECT Query Building a SQL query is as easy as setting a variable to the string that is your SQL query. Of course, you’ll need to use a valid SQL query, or MySQL returns with an error when you execute the query. The variable name $query is used since the name reflects its purpose, but you can choose anything you’d like for a variable name. The SQL query in this example is SELECT * FROM books. Unlike when you used the mysql mandline client, the query does not have a semicolon at the end. You can build up your query in parts using the string concatenate (.) operator: // Assign the query $select = 39。) at the beginning of the line like these: extension_dir = c:/PHP/ext/ extension= This will change the extension to include the directory to C:/php and include the MySQL extension, respectively. You can use the Search function of your text editor 19 to check whether the lines are already there and just need to be unmented, or whether they need to be added pletely. You’ll need to restart Apache, and then MySQL support will be enabled. 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 and, optionally, the database connection. If you don’t specify the database connection, the default is the connection from the last mysql_connect: // Select the database $db_select=mysql_select_db($db_database)。 if (!$connection){ die (Could not connect to the database: br /. mysql_error( ))。)。 If you didn’t create the tables in Chapter 8, the code in Example 93 can be saved as and run from the mand prompt with the following syntax: mysql u username ppassword D database_name Using the values from the examples, it bees: mysql u test pyourpass D test 18 The database is called test, and it consists of three tables called books, authors, and purchases. Each table has a few sample rows. That’s enough to get us started querying from PHP. Connecting to the Database The first thing you need to do is connect to the database and check to make sure there’s a connection. Including the file that you set up to store your connection information allows you to use the variables instead of hardcoded values when you call the mysql_connect function, as shown in Example 94. We’re assembling one file, , by adding these code snippets. Example 94. Including the connection values and calling mysql_connect in // Include our login information include(39。2021021039。 DHTML Cookbook39。,39。),(2,39。,39。,39。 INSERT INTO purchases VALUES (1,39。 CREATE TABLE purchases ( id int(11) NOT NULL auto_increment, user varchar(10) default NULL, title varchar(150) default NULL, day date default NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1。,256)。,476),(2,39。 Dumping data for table books INSERT INTO books VALUES (1,39。 ? Figure 92 illustrates how you’re going to use this file with other PHP files. You’regoing to continue using the database that you started to set up in Chapter 7. Figure 92. Reusing the login details in multiple files Example 93. The SQL to recreate the test objects (continued) DROP TABLE IF EXISTS books。 $db_password=39。 $db_username=39。 17 $db_database=39。 ? In Example 92, we create this file to use a database on the same machine as the web server. We assign it a database name, username, and password. ?php $db_host=39。 $db_password=39。 $db_username=39。 $db_database=39。 versus PEAR’s DB connect function: $connection = DB::connect( The same basic information is present in both mands, but the PEAR function also specifies the type of databases to which to connect. You can connect to MySQL or other supported databases. We’ll discuss both connection methods in detail. In this chapter, you’ll learn how to connect to a MySQL server fromPHP, how to use PHP to access and retrieve stored data, and how to correctly display information to the user. 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 t