Created
January 31, 2014 18:15
-
-
Save umidjons/8739045 to your computer and use it in GitHub Desktop.
Yii: Read DB records row by row in Yii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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