PHP
  Home arrow PHP arrow Page 4 - Utilizing the LIMIT Clause with the Active Record Pattern
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

Utilizing the LIMIT Clause with the Active Record Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-03-31


    Table of Contents:
  • Utilizing the LIMIT Clause with the Active Record Pattern
  • Review: fetching database records using the LIKE clause
  • Enhancing the functionality of the MySQL class with the LIMIT clause
  • Example: fetching database rows with the LIMIT clause

  • 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


    Utilizing the LIMIT Clause with the Active Record Pattern - Example: fetching database rows with the LIMIT clause
    ( Page 4 of 4 )

    To illustrate how the "fetchLimit()" method created earlier can be used to retrieve a specified number of records from a MySQL table, I first need to have one to work with. So, I'm going to use the same "users" table that you saw in some previous tutorials, which contained data about a few people. Here it is:



    Now that the above MySQL table is available for use, it's time to create a short code sample that shows how to utilize the "fetchLimit()" method included in the "MySQL" class. In this specific case, the example below retrieves four users, starting from the third row. Take a look at it, please:

    try{

    // connect to MySQL and select a database

    $db=new MySQL('host','user','password','mydatabase');

    // display users with the LIMIT clause

    $result=$db->fetchLimit(2,4,'users');

    foreach($result as $row){

    echo $row['firstname'].' '.$row['lastname'].' '.$row['email'].'<br />';

    }


    /* displays the following


    Susan Norton susan@domain.com
    Marian Wilson marian@domain.com
    Mary Smith mary@domain.com
    Amanda Bears amanda@domain.com


    */


    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    See how simple it is to use the prior "fetchLimit()" method to fetch a predefined number of records from the sample "users" MySQL table? I bet you do! As show above, the method allows you to perform LIMIT clauses using a modified version of the active record approach, and best of all, through a friendly interface.

    Finally, feel free to introduce your own enhancements into the previous "MySQL" class (possibly checking SQL queries more thoroughly, adding more methods, etc), in this manner extending your skills in utilizing the active record pattern with PHP.

    Final thoughts

    We've come to the end of this fifth installment of the series. Overall the experience has been hopefully instructive, because you learned how to build a basic MySQL abstraction class that performs a few common database-related operations using the active record pattern.

    In this case, no mapper objects were used, as a typical implementation of this pattern would do, but in the end, the class's API permits you to access MySQL tables through a highly abstract and friendly interface, which could be quite useful when working with multiple database servers.

    However, there's a point with reference to the way this class works that deserves a close analysis. Obviously, its methods often duplicate business logic. For instance, why code two different methods that perform SELECT statements (even if they append distinct SQL clauses to the end), when they can be merged into a single one that builds queries dynamically?

    This specific issue, and a few others, will be addressed in the next article, so you don't have any excuses to miss it!



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

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - 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...





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