如果我的存储过程函数 return 只有一个结果,我该如何 return 多个结果?

How do I return multiple results from my Stored Procedure function if it returns only one result?

我现在有一个使用 SQL Server 2017 的应用程序。当我在 SSMS 上执行存储过程时,它会显示所有数据。但是,在 PHP 上,当我执行它时,它只显示出现的第一个数据。

这是命令:

EXEC dbo.SP_Get_Books_Archive;

我使用的存储过程:

CREATE PROCEDURE SP_Get_Books_Archive
AS
BEGIN
SELECT 
   Book_ISBN as ISBN, 
   Book_Name, Book_Author, 
   Category_Name, 
   Book_Status, 
   Book_Copies_Current, 
   Book_Copies_Actual 
FROM Book 
LEFT JOIN Book_Category ON (Book.Book_Category_ID = Book_Category.Category_ID)
END

PHP 我使用的代码:

<?php
$connection = sqlsrv_connect($server, $connectionInfo);
$query = "EXEC SP_Get_Books_Archive";
$statement = sqlsrv_prepare($connection, $query);
$result = sqlsrv_execute($statement);
$row = sqlsrv_fetch_array($statement);
$rowCount = 1;

var_dump($row);
if (count($row) > 0) {
    foreach ($row as $key => $value) {
        if ($key == 'ISBN') {
            echo "<tr>";
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Author') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Category_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Status') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Current') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Actual') {
            echo "<td>" . $value . "</td>";
        }
        if ($rowCount == count($row)) {
            echo "</tr>";
        }
        $rowCount++;
    }
}

(note: this is enclosed on a <?php include(get_books.php) ?> code located on another php file. tried not separating it but no avail)

结果仅表示:

我也尝试在 $query 上执行完整的 SELECT... 脚本,但结果相同。我的 PHP 代码有问题还是我错过了 SQL 服务器错误?

这与 this post 有点相似,但我认为答案不是我的问题。

您需要获取所有数据:

while ($row = sqlsrv_fetch_array($statement, SQLSRV_FETCH_ASSOC)) {
   ...
}

作为附加说明,您需要检查每个 sqlsrv_ 函数调用的结果。一个示例,基于您的代码:

<?php
// Connection
$connection = sqlsrv_connect($server, $connectionInfo);
if ($connection === false ) {
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}

// Statement
$query = "EXEC SP_Get_Books_Archive";
$statement = sqlsrv_prepare($connection, $query);
if ($statement === false) {
    echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
    exit;
}   

// Execution
$result = sqlsrv_execute($statement);
if ($result === false) {
    echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
    exit;
}   

// Fetch data
$rowCount = 0;
while ($row = sqlsrv_fetch_array($statement, SQLSRV_FETCH_ASSOC)) {
    $rowCount++;
    echo "<tr>";
    foreach ($row as $key => $value) {
        if ($key == 'ISBN') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Author') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Category_Name') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Status') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Current') {
            echo "<td>" . $value . "</td>";
        } else if ($key == 'Book_Copies_Actual') {
            echo "<td>" . $value . "</td>";
        }
    }
    echo "</tr>";
}

// End
sqlsrv_free_stmt($statement);
sqlsrv_close($connection);
?>