News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Procurios Blog:
Syntactic Sugar for MySQLi Results using SPL Iterators
May 15, 2009 @ 11:14:31

From the Procurios blog there's a recent post looking at a method letting you use a foreach on the results from a MySQLi request - SPL Iterators.

Ever wondered why you can't use foreach() on MySQLi Results, and instead have to write less convenient while() loops with fetch_row? Actually, you can use foreach() on MySQLi Results. All it takes is some SPL Iterator magic.

The code examples show how to create an Iterator interface (with rewind, current, key, next and valid methods) to create a ResultIterator class for moving back and forth between the values in the result. This allows you to define the new Iterator object and use the foreach structure like you would a normal result set.

They also show how to bypass this whole problem by using a IteratorAggregate in an extension of the MySQLi interface.

0 comments voice your opinion now!
resultset mysqli spl tutorial iterator


blog comments powered by Disqus

Similar Posts

ProDevTips.com: Zend Amf and class mapping with Flash CS3 / AS3

Codewalkers.com: New Tutorial - Coding \"Best Practices\" - or at least \"Better Practices\"

SitePoint PHP Blog: Create a Movie Recommendation App with Prediction.io - Setup

C7Y: Reflections on Designing an IRC Bot in PHP, Part 1

Zend Developer Zone: Decorators with Zend_Form


Community Events

Don't see your event here?
Let us know!


interview yii2 part2 series project programming community framework example php7 composer application symfony list introduction opinion language api podcast laravel

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework