Location: PHPKode > scripts > Limit Result Set Iterator > limit-result-set-iterator/demo.php
<?php

// Example of Use

require_once("PhpMysqlLimitResultSetIterator.php");

// SETUP DB CONNECTION (Optionally)
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "training";
$db_table = "my_table";
$create_table = true;

$conn = mysql_connect($db_host, $db_user, $db_pass) or die("Cannot connect to database");
mysql_select_db($db_name);

if ($create_table) {
	$sql = "DROP TABLE IF EXISTS $db_table";
	mysql_query($sql);
	
	$sql = "
		CREATE TABLE $db_table (
			`id` int(11) NOT NULL auto_increment, 
			`message` varchar(255) collate latin1_general_ci default NULL,  
			PRIMARY KEY  (`id`)
		)
	";
	mysql_query($sql);

	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
	$sql = "INSERT INTO $db_table (message) VALUES ('tes')";	mysql_query($sql);
}

$chunkSize = 1000;
$sql = "select * from $db_table";
$iterator = new PhpMysqlLimitResultSetIterator($conn, $sql, $chunkSize);

foreach ($iterator as $row) {
	echo $row->id . "," . $row->message . "\n";
}
Return current item: Limit Result Set Iterator