PHP
  Home arrow PHP arrow Page 2 - Working with MySQL Result Sets and the Decorator Pattern in PHP
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? 
Google.com  
PHP

Working with MySQL Result Sets and the Decorator Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-09-05


    Table of Contents:
  • Working with MySQL Result Sets and the Decorator Pattern in PHP
  • Handling MySQL result sets
  • Retrieving MySQL result sets without changing the original class
  • Defining multiple decorator classes

  • 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


    Working with MySQL Result Sets and the Decorator Pattern in PHP - Handling MySQL result sets
    ( Page 2 of 4 )

    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()){
            if(count($options)<4){
                throw new Exception('Invalid number of connection
    parameters');
            }
            foreach($options as $parameter=>$value){
                if(!$value){
                    throw new Exception('Invalid parameter
    '.$parameter);
                }
                $this->{$parameter}=$value;
            }
            $this->connectDB();
        }
        // connect to database
        private function connectDB(){
            if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
                throw new Exception('Error connecting to the
    server');
            }
            if(!mysql_select_db($this->database,$this->conId)){
                throw new Exception('Error selecting database');
            }
        }
        // run query
        public function query($query){
            if(!$this->result=mysql_query($query,$this->conId)){
                throw new Exception('Error performing query
    '.$query);
            }
            return new Result($this,$this->result);
        }
    }
    // define 'Result' class
    class Result{
        private $mysql;
        private $result;
        public function __construct(&$mysql,$result){
            $this->mysql=&$mysql;
            $this->result=$result;
        }
        // fetch row
        public function fetchRow(){
            return mysql_fetch_assoc($this->result);
        }
        // count rows
        public function countRows(){
            if(!$rows=mysql_num_rows($this->result)){
                throw new Exception('Error counting rows');
            }
            return $rows;
        }
        // count affected rows
        public function countAffectedRows(){
            if(!$rows=mysql_affected_rows($this->mysql->conId)){
                throw new Exception('Error counting affected rows');
            }
            return $rows;
        }
        // get insert ID
        public function getInsertID(){
            if(!$id=mysql_insert_id($this->mysql->conId)){
                throw new Exception('Error getting ID');
            }
            return $id;
        }
        // seek rows
        public function seekRow($row=0){
            if(!int($row)||$row<0){
                throw new Exception('Invalid result set offset');
            }
            if(!mysql_data_seek($this->result,$row)){
                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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek