2.7. Activating MySQL Support

The basic PHP software consists of a core set of functionality and optional extensions that provide additional functionality. MySQL support is provided by extensions. In PHP 4, MySQL support is provided by default, but beginning with PHP 5.0, you must activate MySQL support before PHP can interact with MySQL databases. (For more information about PHP extensions, see Book V.)

PHP provides two extensions for MySQL support: the mysql extension and the mysqli (MySQL Improved) extension. Which extension you need to activate depends on which version of PHP and MySQL you're using. The mysql extension, available with PHP 4, 5, and 6, provides functions for interacting with MySQL version 4.0 and earlier. The mysqli extension, added in PHP 5, provides functions for interacting with MySQL version 4.1 and later. You can also use the mysql functions with the later versions of MySQL, but they can't access some of the new features added in the later versions of MySQL.

2.7.1. Activating MySQL support on Linux and the Mac OS

MySQL support is activated during PHP installation on Linux and Mac with installation options. The installation options to activate MySQL must be used during Step 4 of the installation to activate MySQL support. MySQL support can't be added later, after PHP is compiled and installed.

Use one of the following installation options:

--with-mysqli=DIR
--with-mysql=DIR

DIR is the path to the appropriate MySQL directory. When using with-mysqli, use the path to the file named mysql_config. When using with-mysql, use the path to the directory where mysql is installed, such as:

--with-mysql=/user/local/mysql

2.7.2. Activating MySQL support on Windows

You activate MySQL by configuring extension lines in the php.ini file, after PHP is installed. In addition, you must place the files that the extension needs in a location where PHP can find the files.

2.7.2.1. Configuring PHP for MySQL support

To configure PHP for MySQL support, perform the following steps:

  1. Open the php.ini file for editing.

  2. Find the list of extensions.

  3. Find the line for the MySQL extension that you want to use, such as

    ;extension=php_mysqli.dll

  4. Remove the semicolon at the beginning of the line.

If a line doesn't exist for the MySQL extension that you want to use, add the line.

2.7.2.2. Setting up the MySQL support files

To provide MySQL support, PHP requires access to two files — php_mysqli.dll and libmysql.dll. You need to place these files in a folder that's in your system path so that PHP can access them. The best way to make the files available is to add the main PHP directory to your system path and then copy the files into the main directory.

You can add the main PHP directory, such as c:php, to your path, as follows:

  1. Go to the Control Panel.

    For instance, choose StartControl Panel.

  2. Click System.

  3. Click the Advanced Tab.

  4. Click Environment Variables.

    The Environment Variables dialog box, shown in Figure 2-2, opens.

    Figure 2-2. Check your environment variables.
  5. Click Path in the System Variables pane.

    You might need to scroll down to find the line for the Path variable.

  6. Click the Edit button.

    The Edit System Variable dialog box, shown in Figure 2-3, opens.

    Figure 2-3. The Edit System Variable dialog box.
  7. Add a semicolon to the end of the existing system path, followed by the path to the main PHP directory, such as ;c:php.

  8. Click OK.

  9. Restart your computer.

When the main PHP directory is included in your system path, copy the required files into the main directory. The extension files are located in the ext subdirectory. Copy one of the following files, depending on which version of MySQL you're using:

extphp_mysqli.dll  (for MySQL 4.1 or later)
extphp_mysql.dll   (for MySQL 4.0 or earlier)

Copy the file into the main PHP directory, such as c:php.

The second required file, named libmysql.dll, should already be located in the main PHP directory. If it isn't, you need to find it and copy it there. If it's not in your PHP directory, it's usually installed with MySQL, so find it in the directory where MySQL was installed, perhaps in a bin subdirectory, such as c:Program FilesMySQLMySQL Server 5.0in.

You can copy the files into a directory that's already in your system path, such as c:windows or c:windowssystem32, rather than change your system path to include the main PHP directory and copy the files to the main directory as described in this section. However, although this method is simpler, with fewer steps, it can lead to problems when you upgrade PHP in the future. The .dll files for all versions have the same name. If you keep the files in the main PHP directory for the current version, rather than copying them into a common directory, you can just replace the entire main directory with the new version. There's no opportunity to mix up the .dll files for different versions.

2.7.3. Checking MySQL support

To check that MySQL is activated, run the test.php script as described in the section "Testing PHP," earlier in this chapter. The output should include a section showing MySQL settings, as shown in Figure 2-4. If a MySQL section doesn't appear in the output, see the next section, "Troubleshooting."

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.173.32