Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 0.87 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to loop through returned SQL results in Yii - nextRecord()
  2. TOTAL
  3. -----
  4. 999
  5.        
  6. ID |NAME
  7. ---|----
  8. 0  | Jon
  9. 1  | Bob
  10. 2  | Sarah
  11. 3  | Pete
  12.        
  13. //create the call to stored proc
  14.  
  15. $command=parent::$db->createCommand("exec sp_showUsers");    
  16.  
  17. //run the query      
  18.  
  19. $dataReader = $command->query();
  20.  
  21. //For the current object assign the row to a variable
  22.  
  23. while (($row = $dataReader->read()) !== false){
  24.  
  25.       //if the row has a total column
  26.  
  27.       if(isset($row['total'])){
  28.            $total = $row['total'];
  29.       }
  30.  }
  31.  
  32. //Test if there is another result
  33.  
  34. $usersExist = $dataReader->nextResult();
  35.  
  36. //$dataReader->next(); - REMOVED AS NOT NEEDED
  37.  
  38. if($usersExist){
  39.      $userTable = array();
  40.      $i = 0;
  41.      while (($userRow = $dataReader->read())!== false){
  42.             //add each row to a temporary array
  43.  
  44.             $userTable[$i] = $userRow['id'];
  45.             $i++;
  46.      }
  47.  }
  48.  
  49.  ...