0

试图从 SQL Server 2008 中的存储过程 (SP) 中获取 3 个结果集时,我有点发疯了。

存储过程

CREATE PROCEDURE db_abbTest
AS
BEGIN
        SET NOCOUNT ON;

        SELECT 1
        SELECT 2
        SELECT 3
        RETURN 0
END
GO

PHP代码

<?php

$serverName = "server";
$connectionInfo = array("UID"=> "user", "PWD" =>"pass", "Database"=>"sample_db");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn )
{
    $sql = "db_AbbTest";
    $r = sqlsrv_query($conn, $sql, null, array(
            'Scrollable' => SQLSRV_CURSOR_STATIC,
            'SendStreamParamsAtExec'    => true
        ));
    var_dump($r);
        while( $obj = sqlsrv_fetch( $r))
        {
              var_dump($obj);
              var_dump(sqlsrv_fetch_array($r));
        }
     echo "Connection established.\n";
}
else
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Close the connection. */
sqlsrv_close( $conn);

SP 返回三个结果(3x 选择语句)但是我似乎无法从查询中获取所有结果。有任何想法吗?我试过while(sqlsrv_fetch_object($r){//do something}) 但没有运气..

4

1 回答 1

1

我设法找到了解决这个问题的方法,我不得不使用一个 while 循环,然后转到下一个结果:

$sql = "{call db_AbbTest}";
$r = sqlsrv_query($conn, $sql, null);
$i =0;
    while( $obj = sqlsrv_fetch_object($r))
    {
            echo $i;
         print_r($obj);
         sqlsrv_next_result($r);
         $i++;
    }
于 2012-05-08T08:09:14.470 回答