Home arrow PHP arrow Page 9 - Database Abstraction With PHP

No News Is Good News - PHP

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.

TABLE OF CONTENTS:
  1. Database Abstraction With PHP
  2. Alphabet Soup
  3. Sultans Of Swing
  4. Independence Day
  5. Different Strokes
  6. The Number Game
  7. Preparing For The Long Haul
  8. Commitment Issues
  9. No News Is Good News
  10. Catch Me If You Can
  11. Once Again, The Headlines
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 64
February 13, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: