H.2 Getting Started

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 Library

This 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:

  1. Log in as the Unix root user.

  2. Change directory to your PHP source directory location using:

    % cd /usr/local/src/php-version_number

    For example, if the version is PHP 5.0.0, type:

    % cd /usr/local/src/php-5.0.0

  3. Configure the PHP installation by running the configure script. Add improved MySQL support and disable regular MySQL support. This step assumes that MySQL 4.1 has been installed previously in the directory /usr/local/mysql and that Apache 2 has been installed in /usr/local/apache2:

    % ./configure --with-apxs2=/usr/local/apache2/bin/apxs \
    
      --without-mysql --with-mysqli=/usr/local/mysql/bin/mysql_config

    If your configure script complains, then try removing mysql_config from the second line:

    % ./configure --with-apxs2=/usr/local/apache2/bin/apxs \
    
      --without-mysql --with-mysqli=/usr/local/mysql/bin/

  4. Compile the PHP scripting engine by running:

    % make

  5. Now that the PHP scripting engine is built, install the PHP engine using:

    % make install

  6. Restart the Apache web server by running the command:

    % /usr/local/apache2/bin/apachectl restart

H.2.2 Migrating to the New Library

Migrating 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:


mysql_escape_string( )

Replace with calls to mysqli_real_escape_string( ).


mysql_pconnect( )

Replace with calls to mysqli_connect( ).


mysql_unbuffered_query( )

Replace with a call to mysqli_real_query( ) and then mysql_use_result( ). These are discussed in the next section.



     
    ASPTreeView.com
     
    Evaluation has ї»СЦ·Чexpired.
    Info...