RowCount 总是 return 0 在 SQL 服务器上 PHP 使用存储过程

RowCount always return 0 on SQL Server with PHP using Stored Procedure

我对这个总是返回 0 的 rowCount() 函数有疑问。我已经尝试使用硬代码,但仍然没有显示结果

{"status":false,"message":"Invalid Get Report Book!"}

这是我的报告book.php

<?php
header("Content-type: application/json");
include_once 'Database.php';
include_once 'master.php';


//$id_book=$_GET['id_book'];
//$TanggalStart = $_GET['TanggalStart'];
//$TanggalEnd = $_GET['TanggalEnd'];


$id_book='KD-BKK-1';
$TanggalStart = '2020-12-22';
$TanggalEnd = '2021-12-31';

// get database connection
$database = new Database();
$db = $database->getConnection();
// prepare province object
$reportbook = new Master($db);
$stmt = $reportbook->GetReportBook($id_book, $TanggalStart, $TanggalEnd);
if($stmt->rowCount() > 0){
    // get retrieved row
    while($row = $stmt->fetch())
    {
        // create array
        $book_arr[]=array(
            "status" => true,
            "id_book" => $row['id_book'],
            "book_name" => $row['book_name'],
            "author" => $row['author'],
            "description" => $row['description'],
            "book_price" => $row['book_price'],
            "id_publication" => $row['id_publication'],
            "id_branch" => $row['id_branch'],
            "qty" => $row['qty'],
            "rental_qty" => $row['rental_qty'],
            "imagename" => $row['imagename'],
            "imagepath" => $row['imagepath'],
            "entry_date" => $row['entry_date'],
        );
    }

}
else{
    $book_arr=array(
        "status" => false,
        "message" => "Invalid Get Report Book!",
    );
}
// make it json format
print_r(json_encode($book_arr));

?>

这是我的 SP GetReportBook(它在 ASP.NET 上工作)

 ALTER Procedure [dbo].[GetReportBook]
(
@id_book varchar(50),
@TanggalStart varchar(50),
@TanggalEnd varchar(50)
)
as
begin
DECLARE @rowcount INT;
BEGIN TRY
    SELECT TOP 100 * FROM Book;
    SET @rowcount = @@ROWCOUNT;
END TRY
BEGIN CATCH
    SELECT TOP 50 * FROM Book;
    SET @rowcount = @@ROWCOUNT;
END CATCH
SELECT @rowcount;
SELECT * FROM Book WHERE
(@id_book=id_book OR id_book = id_book)
AND ((@TanggalStart = 'ALL' Or Convert(Varchar,entry_date,112)>=@TanggalStart) And (@TanggalEnd = 'ALL' Or Convert(Varchar,entry_date,112)<=@TanggalEnd))
ORDER BY id_book ASC
END

我还是不明白这有什么问题。这是 SQL 服务器问题还是其他问题?因为 MySQL 上的 rowCount() 工作正常。

rowCount() 函数不适用于 select:

The number of rows added, deleted, or changed.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, a PDO::CURSOR_FWDONLY cursor returns -1. A PDO::CURSOR_SCROLLABLE cursor returns the number of rows in the result set.

因此,只需读取所有行,然后测试数组大小。

while($row = $stmt->fetch())
{
    // create array
    $book_arr[]=array(
        "status" => true,
        "id_book" => $row['id_book'],
        "book_name" => $row['book_name'],
        "author" => $row['author'],
        "description" => $row['description'],
        "book_price" => $row['book_price'],
        "id_publication" => $row['id_publication'],
        "id_branch" => $row['id_branch'],
        "qty" => $row['qty'],
        "rental_qty" => $row['rental_qty'],
        "imagename" => $row['imagename'],
        "imagepath" => $row['imagepath'],
        "entry_date" => $row['entry_date'],
    );
}

if ($row = 0) {
    $book_arr=array(
        "status" => false,
        "message" => "Invalid Get Report Book!",
    );
}