This section explains how to install the improved MySQL library and the basics of migrating existing code to the new functions. H.2.1 Installing the LibraryThis section discusses installation on only Unix platforms. We assume you have followed our installation instructions in Appendix A through Appendix C, and that you have installed MySQL 4.1, PHP, and Apache and now want to enable the new library. We also assume that you've kept the source code for the components, and stored it in the subdirectories of /usr/local/src/ that are recommended in Appendix A through Appendix C. To configure the improved MySQL library, you need to reconfigure, recompile, and reinstall PHP. You also need to restart your Apache 2 server. To do this, follow these steps:
H.2.2 Migrating to the New LibraryMigrating legacy code to the new library is typically straightforward but requires that all MySQL function calls be modified. The function names in the regular and improved MySQL libraries are different and so are most of the parameter orders. Renaming your functions is easy. Almost all functions in the regular library have an equivalent in the improved library, and the improved library function simply has mysqli_ as its prefix instead of mysql_. For example, mysql_query( ) is replaced by mysqli_query( ). The other significant difference in migrating code is that the connection resource parameter is mandatory to almost all functions and is the first parameter. For example, for mysqli_query( ) the first parameter is the connection handle and the second the SQL query; this is the opposite of mysql_query( ). Consider an example. Example H-1 is a copy of Example 6-1 from Chapter 6. that uses the regular library. It's been rewritten to use the improved library in Example H-2. The changes required are to globally replace mysql_ with mysqli_ and to move the connection handle parameter to be the first parameter in all function calls. Also, the constant MYSQL_NUM is changed to MYSQLI_NUM. Example H-1. A code example that uses the regular MySQL library<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Wines</title> </head> <body> <pre> <?php // (1) Open the database connection $connection = mysql_connect("localhost","hugh","drum"); // (2) Select the winestore database mysql_select_db("winestore", $connection); // (3) Run the query on the winestore through the connection $result = mysql_query ("SELECT * FROM wine", $connection); // (4) While there are still rows in the result set, fetch the current // row into the array $row while ($row = mysql_fetch_array($result, MYSQL_NUM)) { // (5) Print out each element in $row, that is, print the values of // the attributes foreach ($row as $attribute) print "{$attribute} "; // Print a carriage return to neaten the output print "\n"; } ?> </pre> </body> </html> Example H-2. A modified version of Example H-1 that uses the improved MySQL library<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Wines</title> </head> <body> <pre> <?php // (1) Open the database connection $connection = mysqli_connect("localhost","hugh","drum"); // (2) Select the winestore database mysqli_select_db($connection, "winestore"); // (3) Run the query on the winestore through the connection $result = mysqli_query($connection, "SELECT * FROM wine"); // (4) While there are still rows in the result set, fetch the current // row into the array $row while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) { // (5) Print out each element in $row, that is, print the values of // the attributes foreach ($row as $attribute) print "{$attribute} "; // Print a carriage return to neaten the output print "\n"; } ?> </pre> </body> </html> The following popular functions aren't available in the improved library:
|