PHP
  Home arrow PHP arrow Page 4 - Caching Result Sets in PHP: Object Int...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Caching Result Sets in PHP: Object Interaction Within a Caching System
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2005-10-17

    Table of Contents:
  • Caching Result Sets in PHP: Object Interaction Within a Caching System
  • The first link in the caching process: looking at the “Cache” class
  • The second link in the caching process: overview of the “MySQL” class
  • The third link in the caching process: a quick look at the “Result” class
  • Chaining the links: putting the classes together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    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


    (Page 4 of 5 )

    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


       · In this fourth part of the series, the result-set caching class is integrated with...
       · Great set of articles, it's exactly what I was looking for.I am having some...
       · Hello,Thanks for commenting on the article. Regarding your question, the reason...
       · I'm just wondering why not using PEAR :: DB instead of creating new classes for...
       · Thanks for the comments on my article and my classes.In fact, I'm not reinventing...
       · Thank you for answering back.I do see your point, but let me add this. It's true...
       · Thank you for your reply.I understand your point too. What you said about PEAR...
       · I have coded in several different languages prior to using PHP, and have more of a ...
       · I'm glad to know that this series have been quite helpul for you in learning the OOP...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT