Skip to content

Instantly share code, notes, and snippets.

@umidjons
Created January 31, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umidjons/8739045 to your computer and use it in GitHub Desktop.
Save umidjons/8739045 to your computer and use it in GitHub Desktop.
Yii: Read DB records row by row in Yii
<?php
// get data from db:
$cmd = Yii::app()->db->createCommand( "SELECT name, sex, age, position FROM people" );
// get reader object:
$dataReader = $cmd->query();
// read records line by line:
while ( ( $rec = $dataReader->read() ) !== false )
printf( "Name: %s, Sex: %s, Age: %d, Position: %s", $rec[ 'name' ], $rec[ 'sex' ], $rec[ 'age' ], $rec[ 'position' ] );
// or
foreach ( $dataReader as $rec )
printf( "Name: %s, Sex: %s, Age: %d, Position: %s", $rec[ 'name' ], $rec[ 'sex' ], $rec[ 'age' ], $rec[ 'position' ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment