PHP
  Home arrow PHP arrow Page 2 - 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 - Review: fetching database records using the LIKE clause
    ( Page 2 of 4 )

    As I said in the beginning, the sample "MySQL" class that I used in the previous article was capable of fetching rows from a specified database table by using a straightforward method, called simply "fetchLike()."

    It'd be helpful to recall briefly how the signature of this class looked originally, however, before I proceed to extend its existing functionality via the active record pattern. Therefore, below I included the definition of the class, along with a simple example that demonstrates how it can be used to retrieve all the records from a "users" MySQL table whose "firstname" field contain the "a" character. Here it is:


    class MySQL{

    private $result;

    public function __construct($host='localhost',$user='user',$password='password',$database='database'){

    // connect to MySQL and select database

    if(!$conId=mysql_connect($host,$user,$password)){

    throw new Exception('Error connecting to the server');

    }

    if(!mysql_select_db($database,$conId)){

    throw new Exception('Error selecting database');

    }

    }

    // run SQL query

    public function query($query){

    if(!$this->result=mysql_query($query)){

    throw new Exception('Error performing query '.$query);

    }

    }

    // fetch one row

    public function fetchRow(){

    while($row=mysql_fetch_array($this->result)){

    return $row;

    }

    return false;

    }

    // fetch all rows

    public function fetchAll($table='default_table'){

    $this->query('SELECT * FROM '.$table);

    $rows=array();

    while($row=$this->fetchRow()){

    $rows[]=$row;

    }

    return $rows;

    }

    // fetch rows using 'WHERE' clause

    public function fetchWhere($where,$table='default_table'){

    $this->query('SELECT * FROM '.$table.' WHERE '.$where);

    $rows=array();

    while($row=$this->fetchRow()){

    $rows[]=$row;

    }

    return $rows;

    }

    // fetch rows using 'LIKE' clause

    public function fetchLike($field,$like,$table='default_table'){

    $this->query('SELECT * FROM '.$table.' WHERE '.$field.' LIKE '.$like);

    $rows=array();

    while($row=$this->fetchRow()){

    $rows[]=$row;

    }

    return $rows;

    }

    // insert row

    public function insert($params=array(),$table='default_table'){

    $sql='INSERT INTO '.$table.' ('.implode(',',array_keys($params)).') VALUES (''.implode("','",array_values($params)).'')';

    $this->query($sql);

    }

    // update row

    public function update($params=array(),$where,$table='default_table'){

    $args=array();

    foreach($params as $field=>$value){

    $args[]=$field.'=''.$value.''';

    }

    $sql='UPDATE '.$table.' SET '.implode(',',$args).' WHERE '.$where;

    $this->query($sql);

    }

    // delete one or multiple rows

    public function delete($where='',$table='default_table'){

    $sql=!$where?'DELETE FROM '.$table:'DELETE FROM '.$table.' WHERE '.$where;

    $this->query($sql);

    }

    }


    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();

    }


    As you'll recall, the above example illustrates how to utilize the pertinent "MySQL" class to fetch a few trivial records from a "users" MySQL table. In this case, the method used to retrieve this data is called "fetchLike()" and, as its name suggests, performs SELECT queries that include a LIKE clause.

    Naturally, the most important detail to spot here is this method's ability to execute the aforementioned SQL query through a friendly interface that eliminates the need for coding SQL statements from scratch.

    Now that you've recalled how the prior "MySQL" class does its business, it's time to continue extending its existing functionality. As I stated in the introduction, I'm going to define a new method within this class, which will be charged with retrieving rows from a targeted MySQL table by using a LIMIT clause. This could be pretty useful when it's necessary to build a record pagination mechanism.

    This method will be built in the next section, so click on the link shown below and keep reading.



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