Home arrow PHP arrow Page 3 - Handling MySQL Data Set Failures in PHP 5

Triggering custom exceptions when processing MySQL data sets - PHP

If you’re a PHP developer who wants to learn the basics of implementing customized exceptions in PHP 5, then look no further, because you’ve come to the right place. This is the third part of a four-part series entitled “Subclassing exceptions in PHP 5.” It teaches you how to extend the native exception mechanism bundled with PHP 5 by using inheritance, and complements the corresponding theory with copious, illustrative hands-on examples.

  1. Handling MySQL Data Set Failures in PHP 5
  2. Intercepting MySQL-related exceptions with PHP 5
  3. Triggering custom exceptions when processing MySQL data sets
  4. Handling MySQL and result set exceptions with separated try-catch blocks
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 29, 2008

print this article



As I mentioned before, my purpose here is to modify the signature of the "Result" class by providing it with the ability to throw specific exceptions when processing MySQL data sets.

Based on this idea, the pertinent "Result" class would now look as follows:

// redefine 'Result' class

class Result {

private $mysql;

private $result;

public function __construct($mysql,$result){




// fetch row

public function fetchRow(){


return false;


return $row;


// count rows

public function countRows(){


throw new ResultException('Error counting rows');


return $rows;


// count affected rows

public function countAffectedRows(){


throw new ResultException('Error counting affected rows');


return $rows;


// get ID of last-inserted row

public function getInsertID(){


throw new ResultException('Error getting ID');


return $id;


// seek row

public function seekRow($row=0){


throw new ResultException('Invalid result set offset');



throw new ResultException('Error seeking data');




That wasn't rocket science, right? As you can see, the above "Result" class is now capable of throwing some customized exceptions of type "ResultException" each time an error occurs, either when counting and seeking rows in a result set, or when retrieving the ID of an insertion, update or deletion operation.

Of course, these same failures could have been handled through generic exceptions. In this case, however, I want to provide you with a simple example to help you learn how to create classes that trigger customized exceptions.

At this point, I've modified the signature of the previous "Result" class, which now will trigger a special type of exception when something goes wrong. The next step that must be taken consists of creating a subclass that handles these exceptions via a "try-catch" block.

Building such a subclass will be the last topic that I plan to discuss in this article. If you're interested in learning how the class in question will be constructed, please click on the link below and read the next few lines.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: