Home arrow PHP arrow Page 4 - Caching Result Sets in PHP: Object Interaction Within a Caching System

The third link in the caching process: a quick look at the “Result” class - PHP

In this article, we work directly with a standalone caching class, showing how it interacts with other objects. We will work with aggregation and composition to achieve our goals, which include implementing a time expiry based caching mechanism.

TABLE OF CONTENTS:
  1. Caching Result Sets in PHP: Object Interaction Within a Caching System
  2. The first link in the caching process: looking at the “Cache” class
  3. The second link in the caching process: overview of the “MySQL” class
  4. The third link in the caching process: a quick look at the “Result” class
  5. Chaining the links: putting the classes together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
October 17, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Very often it is much better to delegate the responsibility for manipulating database result sets to a separated class, rather that assigning these tasks to the same class that connects to MySQL. That’s the primary reason for the existence of the “Result” class. However, don’t be concerned about it. It only takes care of fetching data, counting table rows and other result-related operations.

Here is the proper list for the “Result” class:

class Result{
            var $mysql; // instance of MySQL object
            var $result; // result set
            // constructor
            function Result(&$mysql,$result){
                        $this->mysql=&$mysql;
                        $this->result=$result;
            }
            // fetch row
            function fetchRow(){
                        return mysql_fetch_array($this-
>result,MYSQL_ASSOC);
            }
            // count rows
            function countRows(){
                        if(!$rows=mysql_num_rows($this->result)){
                                   $this->mysql->isError('Error
counting rows');
                        }
                        return $rows;
            }
            // count affected rows
            function countAffectedRows(){
                        if(!$rows=mysql_affected_rows($this-
>mysql->conId)){
                                   $this->mysql->isError('Error
counting affected rows');
                        }
                        return $rows;
            }
            // get ID from last inserted row
            function getInsertID(){
                        if(!$id=mysql_insert_id($this->mysql-
>conId)){
                                   $this->mysql->isError('Error
getting ID');
                        }
                        return $id;
            }
            // seek row
            function seekRow($row=0){
                        if(!mysql_data_seek($this->result,$row)){
                                   $this->mysql->isError('Error
seeking data');
                        }
            }
}

Although the methods belonging to the above class might be appended to the MySQL class, it’s much more efficient having a separated structure that accomplishes all of the row-related operations. So, as you can see, this class allows us to fetch rows, count result rows, and even get the ID from the last inserted record.

Of course, more methods may be eventually added, useful for improving the functionality within the class. But, as I said before, we’ll keep it simple.

By this point, we’ve covered in detail each class that interacts with “Cache.” The next step is putting all of the pieces together and demonstrating a practical example.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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