【问题标题】:RowCount always return 0 on SQL Server with PHP using Stored Procedure使用存储过程的 PHP 在 SQL Server 上的 RowCount 始终返回 0
【发布时间】:2021-01-19 05:18:29
【问题描述】:

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

{"status":false,"message":"获取报告书无效!"}

这是我的报告书.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 Server 问题还是其他问题?因为MySQL 上的rowCount() 工作正常。

【问题讨论】:

  • 是的,我正在使用 PDO_SQLSRV
  • 我明白了。这个问题的任何解决方案?我已经研究过了,但我还是不明白

标签: php sql-server stored-procedures


【解决方案1】:

rowCount() 函数不适用于select

添加、删除或更改的行数。

如果关联 PDOStatement 执行的最后一条 SQL 语句是 SELECT 语句,则 PDO::CURSOR_FWDONLY 游标返回 -1。 PDO::CURSOR_SCROLLABLE 游标返回结果集中的行数。

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

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!",
    );
}

【讨论】:

  • 未定义属性:第 42 行 C:\xampp\htdocs\library-api\reportbook.php 中的 PDOStatement::$book_arr
  • 好的,先生。我的问题已经解决了。谢谢
猜你喜欢
  • 2019-03-16
  • 1970-01-01
  • 2010-11-23
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多