PHP
  Home arrow PHP arrow Page 4 - Handling MySQL Data Set Failures in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Handling MySQL Data Set Failures in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-10-29


    Table of Contents:
  • Handling MySQL Data Set Failures in PHP 5
  • Intercepting MySQL-related exceptions with PHP 5
  • Triggering custom exceptions when processing MySQL data sets
  • Handling MySQL and result set exceptions with separated try-catch blocks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Handling MySQL Data Set Failures in PHP 5 - Handling MySQL and result set exceptions with separated try-catch blocks
    ( Page 4 of 4 )

    As I explained in the previous section, it's necessary to derive a subclass from the built-in "Exception" class bundled with PHP 5 to separately handle all of the specific exceptions triggered by the "Result" class coded previously. Therefore, I've included the signature of this brand new exception subclass below. It is responsible only for handling some errors that might occur when processing MySQL result sets.

    Here's how this particular class looks:


    // create ResultException class

    class ResultException extends Exception{

    public function __construct($message,$code=0){

    // call parent of Exception class

    parent::__construct($message,$code);

    }

    public function showResultExceptionInfo(){

    return 'Catching Result exceptions...<br />Exception message: '.$this->getMessage().'<br />Source filename of exception: '.$this->getFile().'<br />Source line of exception: '.$this->getLine();

    }

    }


    Undeniably, the definition of the above "ResultException" subclass is pretty simple. It extends the functionality of the corresponding "Exception" parent; it implements a basic method called "showResultExceptionInfo()." This method returns specific information to client code about the launched exception, including its source file, the line of code that triggered the error, and so forth. Nothing too complicated to grasp, right?

    Well, now that we have derived a subclass that handles specific MySQL result set exceptions, please focus your attention on the following script. It demonstrates how to use the subclass. Here it is:

    try{

    // connect to MySQL

    $db=new MySQL(array('host'=>'host','user'=>'user,'password'=>'password','database'=>'database'));

    // fetch data on some users

    $result=$db->query('SELECT * FROM users');

    // display data on some users

    while($row=$result->fetchRow()){

    echo 'First Name: '.$row['firstname'].' Last Name: '.$row['lastname'].' Email: '.$row['email'].'<br />';

    }

    // throw a Result Exception

    echo $result->getInsertID();

     

    /* displays the following

    Catching Result exceptions...

    Exception message: Error getting ID

    Source filename of exception: path/to/file/exception_test.php

    Source line of exception: 93

    */

    }

    // catch MySQL exceptions here

    catch(MySQLException $e){

    echo $e->showMySQLExceptionInfo();

    exit();

    }

    // catch Result exceptions here

    catch(ResultException $e){

    echo $e->showResultExceptionInfo();

    exit();

    }

    // catch default exceptions here

    catch(Exception $e){

    echo 'Catching default exceptions...<br />';

    echo 'Exception message: '.$e->getMessage().'<br />';

    echo 'Source filename of exception: '.$e->getFile().'<br />';

    echo 'Source line of exception: '.$e->getLine();

    exit();

    }


    As you can see from the above hands-on example, the "getInsertID()" method that belongs to the "Result" class is called deliberately. This condition immediately fires up an exception of type "ResultException," which is finally intercepted and handled property by a specific "catch" block.

    Hopefully, the previous example was useful enough to demonstrate how several types of exception can be handled within the same PHP 5 application.

    Feel free to use all of the code examples developed in this tutorial to help you acquire a more solid background in working with exception subclasses within PHP 5-driven applications.

    Final thoughts

    In this third installment of the series, you learned how to combine three different types of exceptions in the same PHP 5 application. In this particular case, the whole process demanded that we first derive two specific subclasses from the base "Exception" class, and then include the corresponding "try-catch" blocks required to handle each kind of exception.

    In the next article, I'm going to conclude this series by demonstrating how to merge the two exception subclasses that you have learned into one, which can be useful if you don't want to clutter the source code of a web application with many "try-catch" blocks.

    Now that you've been warned about the topics that will be discussed in the final article, you won't want to miss it!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT