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

Putting the MySQLi_ResultWrapper class to work - 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

If you’re like me, then you want to learn how to work with the previous “MySQLi_ResultWrapper” class, right? Below I coded a script that shows the benefits of making this class an implementer of the Iterator interface.

The script in question is as follows:

<?php

 

 

try

{

    // connect to MySQL

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

   

    // perform query

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

   

    // traverse rows in result set

    foreach ($result as $user)

    {

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

    }

   

    // reset result pointer

    $result->rewind();

   

    // display result pointer

    echo 'Result pointer : ' . $result->key();

   

    // display data in current row

    $user = $result->current();

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

   

    // move forward result pointer

    $result->next();

   

    // display result pointer

    echo 'Result pointer : ' . $result->key();

   

    // display data in current row

    $user = $result->current();

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

   

    // reset result pointer

    $result->rewind();

}

 

 

// catch exceptions

catch(Exception $e)

{

    echo $e->getMessage();

    exit();

As the above code sample shows, it’s extremely easy to traverse a database result set by using a “foreach” loop. Once the data have been fetched from a sample “users” table, thanks to the implementation of the Iterator interface, traversing the records is a breeze. In addition, the example demonstrates how to explicitly use the “rewind(),” “current()” and “key()” methods, which allow you to freely navigate the result set.

So far, so good. Now that you've grasped the logic driving the previous code sample, it’s time to show how to use the “count(),” “offsetExists()” and “offsetGet()” methods. This example will be the appropriate conclusion to this tutorial, so click on the link that appears below and read the following segment.



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