PHP
  Home arrow PHP arrow Page 4 - Running Conditional Select Statements 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

Running Conditional Select Statements with the Active Record Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-03-17


    Table of Contents:
  • Running Conditional Select Statements with the Active Record Pattern
  • Review: performing CRUD operations through the active record approach
  • Performing conditional SELECTS through the active record pattern
  • Running conditional SELECTS using the sample MySQL 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


    Running Conditional Select Statements with the Active Record Pattern - Running conditional SELECTS using the sample MySQL class
    ( Page 4 of 4 )

    To demonstrate how the “fetchWhere()” method defined in the previous section can be used in a concrete situation, it is necessary to have at our disposal a sample MySQL table with which to work. Therefore, I’m going to use the one that was built in the first article, whose structure looked like this:



    Now that there’s a “users” MySQL table available for testing purposes, here’s the practical example that demonstrates the functionality of the “fetchWhere()” method:

    try{

    // connect to MySQL and select a database

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

    // display users where ID > 5

    $result=$db->fetchWhere('id>5','users');

    foreach($result as $row){

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

    }


    /* displays the following


    Amanda Bears amanda@domain.com
    Jodie Foster jodie@domain.com
    Laura Linney laura@domain.com
    Alice Dern alice@domain.com
    Jennifer Aniston jennifer@domain.com


    */


    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    That was pretty easy to grasp, wasn’t it? By means of the “fetchWhere()” method it is very simple to perform conditional SELECTS, and best of all, no SQL statements need to be written from scratch.

    In addition, it’s fair to mention that this method doesn’t strictly follow the model imposed by the active record pattern. Nevertheless, it does implement a useful variation of it, even though no data mappers are used in this particular case.

    Final thoughts

    Over the course of this third part of this series I illustrated, with an easy-to-grasp MySQL abstraction class, how to perform conditional SELECT queries against a specified database table. Hopefully, the code samples included in this tutorial will inspire you to build your own active record classes. Fun is already guaranteed!

    In the forthcoming article, I’ll explain how to execute LIKE clauses by way of the aforementioned MySQL class. Therefore, now that you know what the next article will be about, 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