PHP
  Home arrow PHP arrow Page 2 - Using Visitor Objects with MySQL Data Sets 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? 
Google.com  
PHP

Using Visitor Objects with MySQL Data Sets in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-08-16


    Table of Contents:
  • Using Visitor Objects with MySQL Data Sets in PHP 5
  • Visiting database result sets: establishing an interaction between visitors and MySQL
  • Revealing the anatomy of a visitor class: creating visitor objects
  • Completing the round trip: defining the structure of a pagination class

  • 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


    Using Visitor Objects with MySQL Data Sets in PHP 5 - Visiting database result sets: establishing an interaction between visitors and MySQL
    ( Page 2 of 4 )

    In order to set up the foundations of the hands-on example that I plan to create, first I'll define two concrete MySQL-related classes. These will allow me to establish a direct interaction between disparate MySQL data sets and the way that they will be displayed as chunked packets on a web document.

    Considering this concept, below you'll find the complete listing of the two MySQL processing classes that I mentioned before. Please, have a look at them:

    // 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 server
        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 row
        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');
            }
        }
        // accept visitor
        public function acceptVisitor(Visitor $visitor){
            $visitor->visitMySQLResult($this);
        }
    }

    As shown above, the first class is simply tasked with handling all the processes required to connect to the MySQL server and run the corresponding queries. Because these features don't bear much discussion here, I suggest you put them from your mind for a moment and turn your attention to the second class.

    Particularly, the "Result" class is a bit more interesting, since it exposes a set of understandable methods aimed at processing MySQL result sets. So far, it's nothing unexpected, right? However, beside the respective methods, handy for counting and seeking rows, finding insertion IDs and so forth, this class presents the following method:

    public function acceptVisitor(Visitor $visitor){
        $visitor->visitMySQLResult($this);
    }

    Now, with the introduction of the method shown above, certainly things get much more interesting. Notice how the "Result" class in question accepts a visitor object, which will be responsible for inspecting some of the properties of this class. On this occasion, and certainly at the risk of being repetitive (I mentioned this concept in the two preceding articles), you should pay strong attention to the way the "acceptVisitor()" method has been defined.

    As you can see, this method accepts a visitor object as the unique input argument, and passes an instance of the "Result" class to the visitor itself via its "visitMySQLResult()" method. When working with visitor objects in PHP, you'll probably see this schema implemented in all cases, thus keep it in mind for further reference.

    Fine, now that you've already seen how the "Result" class listed above is provided with the ability for inputting a visitor object, it's time to leap forward and learn how the corresponding visitor class can be created. Are you still with me? Great, now please read the following section to find out how this will be achieved.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek