Home arrow PHP arrow Page 2 - Working with MySQL Result Sets and the Decorator Pattern in PHP

Handling MySQL result sets - PHP

If you’re one of those avid PHP developers who wants to learn how to apply the decorator design pattern within your Web applications, then this group of articles might be quite appealing to you. Welcome to the second part of the series “Using the Decorator pattern with PHP.” Comprised of two parts, this series walks you through the core concepts of using this pattern in PHP, and shows you its practical edge with numerous hands-on examples.

  1. Working with MySQL Result Sets and the Decorator Pattern in PHP
  2. Handling MySQL result sets
  3. Retrieving MySQL result sets without changing the original class
  4. Defining multiple decorator classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
September 05, 2006

print this article



In order to demonstrate how the decorator pattern can be used for generating different outputs based on the same MySQL result set, first I’ll list two MySQL processing classes, which will be used for returning data sets from a sample database. So, having said that, below are the signatures of this pair of classes:

// define MySQL class
class MySQL{
    private $conId;
    private $host;
    private $user;
    private $password;
    private $database;
    public function __construct($options=array()){
            throw new Exception('Invalid number of connection
        foreach($options as $parameter=>$value){
                throw new Exception('Invalid parameter
    // connect to database
    private function connectDB(){
            throw new Exception('Error connecting to the
            throw new Exception('Error selecting database');
    // run query
    public function query($query){
            throw new Exception('Error performing query
        return new Result($this,$this->result);
// define 'Result' class
class Result{
    private $mysql;
    private $result;
    public function __construct(&$mysql,$result){
    // fetch row
    public function fetchRow(){
        return mysql_fetch_assoc($this->result);
    // count rows
    public function countRows(){
            throw new Exception('Error counting rows');
        return $rows;
    // count affected rows
    public function countAffectedRows(){
            throw new Exception('Error counting affected rows');
        return $rows;
    // get insert ID
    public function getInsertID(){
            throw new Exception('Error getting ID');
        return $id;
    // seek rows
    public function seekRow($row=0){
            throw new Exception('Invalid result set offset');
            throw new Exception('Error seeking data');
    // return result set
    public function getResult(){
        return $this->result;

The two classes listed above shouldn’t be difficult to understand at all, since I used them as examples in previous PHP-related tutorials. In simple terms, these classes allow you to connect to MySQL and return result sets from a selected database, which is very convenient for my purpose of demonstrating how some decorator classes can process a specific result set.

So far, so good. At this stage, I have provided you with a couple of straightforward MySQL processing classes. Now, it’s time to pay strong attention to the next few lines, where I’ll be developing some decorator classes to generate different outputs from a given MySQL data set.

Want to see how this will be done? Then please read the following section.

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