Installing a PEAR Package - mdb2 Package
(Page 3 of 5 )
The next package that we will test is the mdb2 package. Add the following code to a document, save and run it:
<?php
require_once("MDB2.php");
//include 'connx.php';
// Build a DSN string (Data Source Name)
// Required by MDB2::connect()
//$db_driver://$user:$password@$host/$database_name
$url="mysql://root:pass@localhost/cms";
$con = MDB2::factory($url);
if(PEAR::isError($con)) {
die("Error while connecting : " . $con->getMessage());
}else{
echo "Connection with MDB2 was successful";
}
?>
The setup of the second code block is the same as for PEAR::DB which we used before. The only difference, apart from the names, is that we explicitly define the connection URL (previously DSN):
$url="mysql://root:pass@localhost/cms";
Instead of including the connx.php file:
include 'connx.php';
We use the factory method to connect to the database:
$con = MDB2::factory($url);
then we check for any errors while attempting to connect and write out the appropriate message:
if(PEAR::isError($con)) {
die("Error while connecting : " . $con->getMessage());
}else{
echo "Connection with MDB2 was successful";
}
Next: Mail Package >>
More PEAR Articles Articles
More By David Web