简体   繁体   English

如何遍历返回的SQL结果-nextRecord()

[英]How to loop through returned SQL results - nextRecord()

I have a stored procedure that returns multiple result sets from an MSSQL database. 我有一个存储过程,可以从MSSQL数据库返回多个结果集。 The problem I'm having is looping through each result set. 我遇到的问题是遍历每个结果集。 It seems Yii is not advancing to the next result set to iterate through the rows. Yii似乎没有前进到下一个结果集以遍历行。 Here is an example: 这是一个例子:

RESULT 1: 结果1:

TOTAL
-----
999

RESULT 2: 结果2:

ID |NAME
---|----
0  | Jon
1  | Bob
2  | Sarah
3  | Pete

And here's my attempt at making this work in Yii. 这是我在Yii进行这项工作的尝试。

//create the call to stored proc

$command=parent::$db->createCommand("exec sp_showUsers");    

//run the query      

$dataReader = $command->query();

//For the current object assign the row to a variable

while (($row = $dataReader->read()) !== false){

      //if the row has a total column

      if(isset($row['total'])){
           $total = $row['total'];
      }
 }

//Test if there is another result 

$usersExist = $dataReader->nextResult();

//$dataReader->next(); - REMOVED AS NOT NEEDED

if($usersExist){
     $userTable = array();
     $i = 0;
     while (($userRow = $dataReader->read())!== false){
            //add each row to a temporary array

            $userTable[$i] = $userRow['id'];
            $i++;
     }
 }

 ...

This doesn't seem to loop through the second result set even though the ->next() method has been called? 即使已调用->next()方法,这似乎也无法遍历第二个结果集? Any help would be very appreciated! 任何帮助将不胜感激! Thanks. 谢谢。

Ps The stored procedure does work and I can loop through the results using ordinary PHP and the sqlsrv_next_result() method. Ps存储过程确实起作用,我可以使用普通的PHP和sqlsrv_next_result()方法遍历结果。

In the code, why 在代码中,为什么

$dataReader->nextResult() will return a CDbDataReader Object. $dataReader->nextResult()将返回CDbDataReader对象。

So, you are using $dataReader->next(); 因此,您正在使用$dataReader->next(); this will move the pointer of first result only. 这只会移动第一个结果的指针。 You have to move the pointer returned by nextResult() call. 您必须移动nextResult()调用返回的指针。

I think, 我认为,

$dataReader=$dataReader->next();

will solve the problem 将解决问题

I found the answer. 我找到了答案。 It was the way we were binding the params to our stored procedure that was messing up our results. 我们将参数绑定到存储过程的方式弄乱了我们的结果。 We don't need the $dataReader->next(); 我们不需要$ dataReader-> next(); at all. 完全没有 so all is working ok with the code above. 所以上面的代码都可以正常工作。 I hope this helps someone out who is using SQL server stored procedures with Yii. 我希望这对使用Yii使用SQL Server存储过程的人有所帮助。 :) And just in case anyone needs the stored procedure call example: :)以防万一有人需要存储过程调用示例:

$command=parent::$db->createCommand("exec sp_showUsers :pageNumber, :pageSize, :sortOrder, :viewAll");

$command->bindParam(":pageSize", $pageSize, PDO::PARAM_INT); 
$command->bindParam(":pageNumber", $pageNumber, PDO::PARAM_INT); 
$command->bindParam(":sortOrder", $sortOrder, PDO::PARAM_STR); 
$command->bindParam(":viewAll", $viewAll, PDO::PARAM_BOOL); 

$dataReader = $command->query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM