DatabaseIterator use TableIterator, RowIterator and ColumnIterator classes to manipulate database using PHP-SPL features
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


DatabaseIterator is a class that use new features of PHP5 (SPL, Iterators, magic methods, ...) to simplify process of iteration with a database. Using a DatabaseIterator, you can work with your database the same mode that you work with arrays.

Quick sample usage:

$dbIt = new DatabaseIterator($conn);

foreach($dbIt as $table) { // loop tables
    $columns = $table->getColumns(); // get columns

    foreach($table as $row) { // loop rows
        echo $row; // call to toString() magic method
        // or loop columns
        foreach($columns as $col) {
            echo $row->{$col->name} . PHP_EOL;

More detailed documentation:

Create an ADOdb connection (ADOConnection) and bind to a DatabaseIterator object to list all tables:


$conn = ADONewConnection('mysql');
$conn->PConnect('localhost', 'root', 'pass', 'database');

$databaseIt = new DatabaseIterator($conn);
foreach($databaseIt as $table) {
    echo $table->name . PHP_EOL;

Retrieving the syntax for CREATE:

echo $databaseIt['contents']->getCreateTable();
echo $databaseIt->from('contents')->getCreateTable();

Inserting data:

for($i=0; $i<10; $i++) {
    $std = new stdClass();
    $std->id = ($i+1);
    $std->title = 'Title ' . $i;
    $std->description = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';


// other syntax
$row = $databaseIt['events']->newRow();

Updating/deleting a row:

$databaseIt['events'][0]->summary = 'Hello';

Set a internal SQL and list rows using a each loop:

$databaseIt['contents']->select('pk_content, title')
                       ->where('title REGEXP ""');
$foo = create_function('$item', 'echo $item->title . PHP_EOL;');

Use ADOdb transactions to rollback or commit operations:


foreach($rows as $row) {
    $row->permalink = preg_replace('/[^a-zA-Z0-9_\-\/\.]/', '', $row->permalink);

$conn->RollbackTrans(); // rollback changes
//or $conn->CommitTrans(); if all OK

More documentation: