Location: PHPKode > scripts > Limit Result Set Iterator > limit-result-set-iterator/PhpMysqlLimitResultSetIterator.php
<?php
/**
 * Implementation example for mysql_*
 * 
 * Example Code
 * 
 * $sql = "select id, message from my_large_table";
 * $chunkSize = 1000;
 * $iterator = new PhpMysqlLimitResultSetIterator($sql, $chunkSize);
 * foreach ($iterator as $eachRow) {
 *      echo $eachRow->message . "\n";
 * }
 * 
 * @version 0.1
 * @author hide@address.com
 * @see LimitResultSetIterator.php
 * 
 */

require_once("LimitResultSetIterator.php");

class PhpMysqlLimitResultSetIterator extends LimitResultSetIterator implements Iterator
{
	var $db;
	
	function PhpMysqlLimitResultSetIterator($dbconn, $sql, $buffSize = 10000)
	{
		parent::LimitResultSetIterator($sql, $buffSize);
		$this->db = $dbconn;
	}
	
	/**
	 * @override
	 */
	function executeLimitedSelect($sql, $size, $offset)
	{
		return @mysql_query('select * from (' . $sql . ') mysubquery limit ' . $offset . ',' . $size, $this->db);		
	}

	/**
	 * @override
	 */
	function closeRs($rs)
	{
		if ($rs) {
			@mysql_free_result($rs);
		}
	}
	
	/**
	 * @override
	 */
	function getCurrentRow($rs)
	{
		return @mysql_fetch_object($rs);
	}
}
Return current item: Limit Result Set Iterator