Home arrow PHP arrow Page 4 - The LIKE Clause and the Active Record Pattern

The fetchLike() method in action - PHP

In most cases, the implementation of the active record pattern in PHP (and other programming languages too) is carried out through a few data mapper objects, which are used to perform CRUD operations on a group of targeted database tables. This seven-part article series describes the advantages of using the active record pattern in a variety of situations, and shows you how to do it.

TABLE OF CONTENTS:
  1. The LIKE Clause and the Active Record Pattern
  2. Review: conditional SELECT queries with the active record pattern
  3. Working with LIKE clauses
  4. The fetchLike() method in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: