Getting the PEAR DB Module


Besides the individual support for various database servers, PHP also supports the DB module, which gives you a level of abstraction that smoothes over the details of working with different database servers. When you use DB, you can use the same functions to access your data; if you switch database server types, you just have to change the name of the database sever you're using.

DB is a PHP extension managed by PEAR, the PHP Extension and Application Repository. If you compiled PHP yourself, PEAR was already installed as pear.php in your PHP directory, and if not, there's a script that will install PEAR for you at go-pear.org. To run PEAR, change to the PHP directory and type pear at the command line.

In Windows, PEAR comes with the manual installation version (not the Windows installer version). There should be a .bat file named go-pear.bat in your PHP directory (e.g., C:\PHP); run it by entering go-pear at the command line or by double-clicking go-pear.bat. This .bat file will ask you a few questions and then create pear.bat, which you use to run PEAR.

There are a number of PEAR modules, including the DB module, that are loaded by default when PEAR is loaded, so if you have PEAR, you should have them already (in Windows, go-pear.bat offers to install them for you). To check what modules you have installed, run PEAR and give it the list command like this:

 %php>pear list INSTALLED PACKAGES: =================== PACKAGE         VERSION STATE Archive_Tar     1.2     stable Console_Getopt  1.2     stable DB              1.6.5   stable Mail            1.1.3   stable Net_SMTP        1.2.6   stable Net_Socket      1.0.2   stable PEAR            1.3.1   stable PHPUnit         1.0.1   stable XML_Parser      1.2.0   stable XML_RPC         1.1.0   stable 

This lists the PEAR modules that are already installed, and you can see the DB module among them. If you don't see DB among the installed modules, you can install it with the install DB command, which looks like this:

 %pear install DB 

If you want to find out what other modules are available in the PEAR repository, use the list-all command:

 %pear list-all ALL PACKAGES: ============= PACKAGE                   LATEST   LOCAL APC                         2.0.4 Cache                       1.5.4 Cache_Lite                  1.3.1 apd                         1.0 memcache                    1.3 perl                        0.6 PHPUnit                     1.0.1    1.0.1 PHPUnit2                    2.0.2 PHP_Compat                  1.1.0 Var_Dump                    1.0.0 Xdebug                      1.3.2 Archive_Tar                 1.2      1.2 bz2                         1.0 Contact_Vcard_Build         1.1 Contact_Vcard_Parse         1.30 File_Fstab                  2.0.0 File_HtAccess               1.1.0 File_Passwd                 1.1.0 File_SMBPasswd              1.0.0 MP3_ID                      1.1.3 zip                         1.0 Auth                        1.2.3 Auth_HTTP                   2.0 Auth_PrefManager            1.1.3 Auth_RADIUS                 1.0.4 Auth_SASL                   1.0.1 radius                      1.2.4 Benchmark                   1.2.1 Config                      1.10.2         .         .         . 

If you want to read the PEAR help file, enter "pear help".

To actually use a PEAR module in PHP, you can use the require statement in your code; for example, this statement loads the DB support:

 require 'DB.php'; 

You can also use "include 'DB.php';", but that's slightly differentif a file you want to use doesn't exist, require considers that a fatal error and ends the script, while include just issues a warning. We'll put the DB module to work in the next chunk.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net