One of the nicest things about Perl - the DBI module - finallymakes an appearance in PHP. Take a look at the PEAR database abstractionlayer, by far one of the coolest PHP widgets out there.
You may remember, from your misadventures with MySQL, that MySQL's API includes a function called mysql_error(), which returns information on the last error that occurred. While this is not really something you would miss - I mean, how can you love someone who always brings you bad news? - it's still useful to have lying around, in the event you need it within a script or application.
With the database abstraction layer, though, you're insulated from PHP's native functions - so how do you handle errors? Are your users doomed to spend eternity staring at a cryptic error message or even (joy!) a blank screen? Or is there a white knight who will gallop up and save them from this hell?
As it turns out, there is. The PEAR abstraction layer comes with a bunch of methods designed specifically to simplify the task of handling errors. Take a look at the next example, which demonstrates how they can be used in a PHP script:
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the DB abstraction layer
include("DB.php");
// connect to the database
$dbh = DB::connect("mysql://john:doe@localhost/db287");
// execute query (note the error!)
$query = "RE-ELECT * FROM cds";
$result = $dbh->query($query);
// if an error occurred
if ($dbh->isError($result))
{
// display an error message and exit
echo "Take cover. Something bad just happened.";
exit();
}
// or...
else
{
// iterate through rows and print column data
// in the form TITLE - ARTIST
while($row = $result->fetchRow())
{
echo "$row[1] - $row[2]\n";
}
}
// close database connection
$dbh->disconnect();
?>
The theory here is that the isError() method can be used to
find out if an error occurred during query execution, and display an error message appropriately. You'll notice that I've deliberately introduced a syntax error into the query string above, just to see how true this really is. Here's the output:
Take cover. Something bad just happened.
Hmmm. Guess it works after all. But how about a more
descriptive error message, one that actually tells you what went wrong?
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the DB abstraction layer
include("DB.php");
// connect to the database
$dbh = DB::connect("mysql://john:doe@localhost/db287");
// execute query
$query = "RE-ELECT * FROM cds";
$result = $dbh->query($query);
// if an error occurred
if ($dbh->isError($result))
{
// display an error message and exit
echo "Take cover. Something bad just happened.\n";
echo "You said: $query\n";
echo "The database said: " . DB::errorMessage($result) . "\n";
exit();
}
// or...
else
{
// iterate through rows and print column data
// in the form TITLE - ARTIST
while($row = $result->fetchRow())
{
echo "$row[1] - $row[2]\n";
}
}
// close database connection
$dbh->disconnect();
?>
In this case, the error message has been beefed up with a
more descriptive error string, accessible via the errorMessage() method of the DB class. Here's what it looks like:
Take cover. Something bad just happened.
You said: RE-ELECT * FROM cds
The database said: syntax error