A PEAR CMS: Examining the Code - Connecting to the Database
(Page 2 of 4 )
Next we actually attempt to connect to the database:
// Creates a database connection object in $db
// or, a database error object if it went wrong.
// The boolean specifies this is a persistent
// connection like mysql_pconnect(), it
// defaults to FALSE.
$db = DB::connect($dsn, TRUE);
As the comments point out, the db::connect() function creates a database connection object in $db, or if there is an error, it creates an error object. The next code block then checks to see if an error occurred during the connection attempt:
// Check whether the object is a connection or
// an error.
// Print out a message and exit if it's
// an error object.
if (DB::isError($db)) {
die($db->getMessage());
}
It is always worth checking to see if any errors have occurred; as you will see, I always check for errors when using DB.
Before we move on to look at the other application-wide scripts, just take a look at the front page of the Content Management System application:
The two scripts that are responsible for giving this application the neat appearance include the Pear.css script, that has the following styles: