Pages

Wednesday 16 November 2011

Installing PEAR Packages on Windows

Ok, so now that you've read my previous post about setting PEAR up on Windows, it's time to start installing some packages that will help you on your way.

An absolute must for any sort of database connection is the DB package.  You could argue that the built in database connectivity from the PHP modules is good enough, and you would be right.  There is a certain degree of flexibility in using these packages though, as they are continuously updated throughout the year by a large community (as is PHP), but in some cases it's all about how you interact with your database connectivity as you will see, it's certainly more class driven in how it's all written.  For example, there is support for scalar parameters as shown below (taken from the PEAR documentation):

<?php 
// Create a valid DB object named $db at the beginning of your program...
require_once 'DB.php';
$db =& DB::connect('mysql://usr:pw@localhost/dbnam');
if (
PEAR::isError($db)) {
    die(
$db->getMessage());
}

// Once you have a valid DB object named $db... 
$sql  'select * from clients where clientid = ?'; 
$data 53; 
$res =& $db->query($sql$data);

// Always check that result is not an error

if (PEAR::isError($res)) {
    die(
$res->getMessage());
}
 

?>
This has certain other benefits including the potential for easy cross-platform availability (bar the changes to the SQL syntax that change between SQL servers).  One thing that PHP 5.x users will need to do is replace the '=&'s that tend to crop up as they are now depreciated because of PHP 5.x; these can just be replaced by a simple '='.  If you have errors switched on (which I would at this point during installation and testing), you will most definitely see these being pointed out as errors.
The full list of functions is available under the documentation for each of the specific packages, and so the many other functions for the DB package are listed here.
So now that you know how to use it, it's time to install it.  Launch a Command Prompt, and browse to your PHP folder.  Once there, type 'pear install DB' and press return.  Congratulations.  You've now installed the DB package, it really is that simple.  To use it, you will need to have either an include or a require (as shown in the example above) to reference the DB package (DB.php).

Other packages within PEAR tend to reference this class in some way so it's always a useful one to install.  At the time of writing, DB has been superseded by MDB2 and so that is something that is also worth a view.

No comments:

Post a Comment