PHP
  Home arrow PHP arrow Page 4 - The LIKE Clause and 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

The LIKE Clause and the Active Record Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-03-24


    Table of Contents:
  • The LIKE Clause and the Active Record Pattern
  • Review: conditional SELECT queries with the active record pattern
  • Working with LIKE clauses
  • The fetchLike() method in action

  • 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


    The LIKE Clause and the Active Record Pattern - The fetchLike() method in action
    ( Page 4 of 4 )

    In the previous section, I explained how to aggregate a new method to the sample “MySQL” class to provide it with the capacity for performing SELECT statements that include a LIKE clause. However, the best way for you to understand how this enhanced version of the class functions is by means of a concrete example.

    With that idea in mind, I’m going to use the sample “users” MySQL table created in a previous tutorial of this series, which looked like this:



    Now that the above MySQL table is available for testing purposes, here’s a simple example that shows how to use the “fetchLike()” method defined earlier to fetch all the users whose first names contain the “a” character:


    try{

    // connect to MySQL and select a database

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

    // display users where first name contains the 'a' character

    $result=$db->fetchLike('firstname',"'%a%'",'users');

    foreach($result as $row){

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

    }


    /* displays the following


    Alejandro Gervasio alejandro@domain.com
    Susan Norton susan@domain.com
    Marian Wilson marian@domain.com
    Mary Smith mary@domain.com
    Amanda Bears amanda@domain.com
    Laura Linney laura@domain.com
    Alice Dern alice@domain.com


    */


    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Undoubtedly, you’ll have to agree with me that the above example is very simple to grasp, since it uses the aforementioned “fetchLike()” method to perform a basic SELECT query that includes the LIKE clause. Obviously, regardless of its simplicity, the example is helpful for illustrating how to use the active record approach to execute different types of queries by means of a simplified, abstract interface.

    As usual, feel free to introduce your own modifications to the code samples included in this tutorial. Doing this will surely give you a more solid grounding in using this popular design pattern in PHP 5.

    Final thoughts

    In this fourth chapter of the series, you hopefully learned how to use a basic MySQL abstraction class to perform SELECT statements in conjunction with simple LIKE clauses via the active record approach. As you saw before, this process only required coding a straightforward method and putting it to work. It was that simple, really.

    In the forthcoming episode, I’m going to demonstrate how to utilize the same “MySQL” class coded earlier for running SELECT queries that contain a LIMIT clause.

    The topic will be juicy, so if you wish to master it in a few steps, then don’t miss the next part!



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek