Home arrow PHP arrow Page 2 - Using Visitor Objects with MySQL Data Sets in PHP 5

Visiting database result sets: establishing an interaction between visitors and MySQL - PHP

If you’re looking for brand new material to help you expand your background in using design patterns with PHP 5, then hopefully this article will suit your needs. Welcome to the final installment of the series “Introducing Visitor Objects in PHP 5.” Composed of three chapters, this series walks you through the key points of creating and using visitor objects with PHP 5, and teaches you how to use them in the context of real-world applications.

  1. Using Visitor Objects with MySQL Data Sets in PHP 5
  2. Visiting database result sets: establishing an interaction between visitors and MySQL
  3. Revealing the anatomy of a visitor class: creating visitor objects
  4. Completing the round trip: defining the structure of a pagination class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
August 16, 2006

print this article



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()){
            throw new Exception('Invalid number of connection
        foreach($options as $parameter=>$value){
                throw new Exception('Invalid parameter
    // connect to server
    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 row
    public function seekRow($row=0){
            throw new Exception('Invalid result set offset');
            throw new Exception('Error seeking data');
    // accept visitor
    public function acceptVisitor(Visitor $visitor){

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){

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

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: