Home arrow PHP arrow Page 4 - Building Dynamic Queries with Chainable Methods

Fetching database rows with the chainable orderby() method - PHP

Welcome to the fifth part of a 12-part series focused on method chaining in PHP 5. Through a set of comprehensive and easy-to-follow tutorials, this series of articles shows you how to create and use chainable methods within your own classes. It also teaches you how to implement this useful programming methodology for developing real-world web applications.

TABLE OF CONTENTS:
  1. Building Dynamic Queries with Chainable Methods
  2. Review: dynamically building SQL queries with method chaining
  3. Working with ORDER BY clauses
  4. Fetching database rows with the chainable orderby() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 04, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the section that you just read, the best way to understand how to use the chainable “orderby()” method created in the previous section is by way of a concrete example. Therefore, below I included a simple code sample that shows how to fetch some ordered records from a MySQL table by using the aforementioned method. Look at it:

try

{

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

// fetch result set by chaining methods of MySQL class

$result = $db->select('properties')->where("title LIKE 'c%'")->orderby('title DESC')->fetch();

while ($row = mysqli_fetch_object($result)){

echo $row->title . $row->description. '<br />';

}

}

catch(Exception $e)

{

echo $e->getMessage();

exit();

}

As you can see, the previous code snippet first connects to MySQL, and then retrieves a group of rows from a “properties” MySQL table. Naturally, the most important detail to note here is that the retrieval query has been built by chaining three different methods of the “MySQL” class.

Hopefully, this short example will give you a clear idea of how useful method chaining can be for building tight and compact APIs in PHP 5. As with many of my articles on PHP development, feel free to tweak all of the code samples included in this tutorial, which surely will arm you with a more solid knowledge of how to use this programming approach.

Final thoughts

In this fifth part of the series, I extended the existing functionality of the sample MySQL abstraction class by adding to it another chainable method, for creating the ORDER BY portion of SELECT statements.

In the forthcoming installment, I’m going to finish building this class by defining two final methods. The first one will be tasked with creating the LIKE part of a query, while the second one will be responsible for returning Singletons of the class.

So here’s my recommendation: don’t miss the next tutorial!



 
 
>>> 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: