Home arrow PHP arrow Page 4 - Coding Examples of the Iterator, Countable and ArrayAccess SPL Interfaces in PHP 5

Final script: counting rows in a result set and accessing specified records - PHP

Welcome to the final installment of a series that provides an overview of the Iterator, Countable and ArrayAccess SPL interfaces in PHP 5. Made up of six parts, this series teaches you how to build a fully-working MySQL abstraction class that takes advantage of the methods inherited from these native interfaces to manipulate database record sets easily by using an array syntax.

TABLE OF CONTENTS:
  1. Coding Examples of the Iterator, Countable and ArrayAccess SPL Interfaces in PHP 5
  2. Review: the MySQLiWrapper and MySQLi_ResultWrapper classes
  3. Putting the MySQLi_ResultWrapper class to work
  4. Final script: counting rows in a result set and accessing specified records
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 19, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the section that you just read, I’m going to finish this tutorial by coding an example that demonstrates how to use the “count(),” “offsetExists()” and “offsetGet()” methods implemented by the “MySQLi_ResultWrapper” class. These will allow you to count rows in a given result set, as well as access specified elements in it.

The code sample that performs all of these tasks is as follows:

<?php

 

 

try

{

    // connect to MySQL

    $db = MySQLiWrapper::getInstance(array('host', 'user', 'password', 'database'));

   

    // perform query

    $result = $db->runQuery('SELECT * FROM users');

       

    // count the number of rows in result set

    echo 'Number of rows: ' . $result->count();

   

    // check if given offset exists

    if ($result->offsetExists(1))

    {

        echo 'Row exists!';

    }

    else

    {

        echo 'Row does not exists!';

    }

   

    // get a row according to a given offset

    $user = $result->offsetGet(1);

    echo '<p>First Name: ' . $user->fname . ', Last Name : ' . $user->lname . '</p>';

}

 

 

// catch exceptions

catch(Exception $e)

{

    echo $e->getMessage();

    exit();

}  

Once again, the earlier code fragment shows in a nutshell why implementing the Countable and ArrayAccess SPL interfaces is so convenient. In this particular case, the methods inherited from the interfaces are used to determine the number of rows contained in a data set, and to access a specific record by its offset.

And with this last example, I’m finishing this article that explores the Iterator, Countable and ArrayAccess interfaces packaged with the Standard PHP Library. Feel free to edit all of the code fragments shown in this article. Doing this will surely give you more ideas for how to implement these interfaces in a few other creative ways.

Final thoughts

Sad but true, we’ve come to the end of this series. Hopefully, the code samples shown in its six parts will give you a good idea of how to implement the Iterator, Countable and ArrayAccess SPL interfaces within different classes.

As you saw for yourself, in this particular case the interfaces were implemented by a class that processes MySQL result sets, but naturally they can be used for constructing classes that will handle different data collections, such as XML nodes and flat text files rows. The possibilities and endless.

See you in the next PHP 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: