In Chapter 7, "Using PHP with MySQL," I introduced the standard set of PHP functions used to interact with a MySQL database. These functions have been around for years and will certainly get the job done. But new to PHP as of version 5 is the Improved MySQL Extension functions, designed to take advantage of features added in MySQL 4.1 (they specifically work with MySQL 4.1.3 and later). If your server meets these requirements, you may want to consider using these functions in your PHP applications. Generally speaking, they are both faster and more secure than the basic MySQL functions. To start, connect to MySQL using the mysqli_connect() function. $dbc = mysqli_connect ('localhost', 'username, 'password'); As with the mysql_connect() function, this one returns a resource representing the database connection. This resource will be used as an argument in most of the other mysqli functions. But unlike the mysql_connect() function, mysqli_connect() will let you also specify the database to select: $dbc = mysqli_connect ('localhost', 'username, 'password', 'database'); Or you can use the mysqli_select_db() function: mysqli_select_db($dbc, 'database'); If a connection problem occurred, you can call the mysqli_connect_error() function, which returns the connection error message. mysqli_connect_error(); Note that this function is only for connection errors. The errors resulting from running queries can be reported by the mysqli_error() function: mysqli_error($dbc); Once you've connected to MySQL and selected a database, you run queries using the mysqli_query() function: $result = mysqli_query($dbc, $query); Notice that the syntax of this function places the database connection as the first argument and the query as the second, which is the opposite of the standard mysql_query() function. You'll also notice that whereas the database connection is an optional argument in most of the mysql functions, it's required with the Improved MySQL Extension. To handle the results returned by a SELECT query, use mysqli_fetch_array() in a while loop: while ($row = mysqli_fetch_array ($result)) { // Do whatever with $row. } As with mysql_fetch_array(), you can add a second parameter when calling this function, dictating how the returned array should be indexed. The options are: MYSQLI_NUM, for numeric (starting at 0); MYSQLI_ASSOC, for strings (using the column names); and MYSQLI_BOTH, for both. You can count the number of returned records with $num = mysqli_num_rows($result); And once you are finished with a query's results, you can release them and close the database connection: mysqli_free_result($result); mysqli_close($dbc); These are the most important of the mysqli_* functions. There is also: mysqli_real_escape_string() for escaping problematic characters; mysqli_affected_rows(), which returns the number of rows affected by an INSERT, UPDATE, or DELETE query; a few functions related to using transactions, which will be used in Chapter 14, "ExampleE-commerce"; and about two or three dozen others, all listed in the PHP manual. As you can see, for the most part you'll use these functions exactly as you do the standard mysql_* functions. As a quick demonstration of this, I'll write a script that retrieves all of the comments stored in the test database. The specific table was created and the comments were added at the beginning of Chapter 10, "Web Application Security"; if you do not have a populated comments table, read that section first. Also, make sure that you are running PHP 5 or later with MySQL 4.1.3 or later. And if you do not know how to enable mysqli support in PHP, see Appendix A. To use the Improved MySQL Extension
Tips
|