【文章內(nèi)容簡(jiǎn)介】
_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 (!$db_select){die (Could not select the database: br /. mysql_error( ))。}Again, it’s good practice to check for an error and display it every time you access the database. Now that you’ve got a good database connection, you’re ready to execute your SQL query.7 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.8 Executing the QueryTo 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: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.9 Fetching and Displaying Use mysql_fetch_row to get the rows from the result set. Its syntax is:array mysql_fetch_row ( resource $result)。It takes the result you stored in $result fromthe query as a parameter. It returns one row at a time from the query until there are no more rows, and then it returns FALSE. Therefore, you do a loop on the result of mysql_fetch_row and define some code to display each row: The columns of the result row are stored in the array and can be accessed one at a time. The variable $result_row accesses the second attribute (as defined in the query’s column order or the column order of the table if SELECT * is used) in the result row.10 Fetch types This is not the only way to fetch the results. Using mysql_fetch_array, PHP can pl