PHP/sqlsrv_query 资源 ID 为 3 但没有 return 数据

PHP/sqlsrv_query resource id is 3 but does not return data

我有一个简单的存储过程 -

CREATE PROCEDURE [dbo].[usp_ws_s_GetCargoClearanceApproval_t]
    @BOLID INT,
    @ACCOUNTID INT,
    @TOTALAMOUNT FLOAT OUT,
    @STORAGEDAYS INT OUT,
    @ACCOUNTCREDIT FLOAT OUT,
    @CREDITLIMIT FLOAT OUT
AS

BEGIN
--SET NOCOUNT ON
    SET @TOTALAMOUNT= '25.00'; SET @STORAGEDAYS = 5; SET @ACCOUNTCREDIT =2500.00;  SET @CREDITLIMIT=75000.00 ;
    SELECT @TOTALAMOUNT,@STORAGEDAYS,@ACCOUNTCREDIT,@CREDITLIMIT;
END

这是由php文件调用的,代码是-

<?php
$bol_id = 426863;
$account_id = 26293;
$totalamount = 0.00;
$storagedays = 0;
$accountcredit = 0.00;
$creditlimit = 0.00;

$param1 = array($bol_id, SQLSRV_PARAM_IN);
$param2 = array($account_id, SQLSRV_PARAM_IN); 

$param3 = array($totalamount, SQLSRV_PARAM_OUT); 
$param4 = array($storagedays, SQLSRV_PARAM_OUT);
$param5 = array($accountcredit, SQLSRV_PARAM_OUT); 
$param6 = array($creditlimit, SQLSRV_PARAM_INOUT);


$params = array($param1, $param2, $param3, $param4, $param5, $param6);

$tsql = "{call usp_ws_s_GetCargoClearanceApproval_t(?,?,?,?,?,?)}";  

$Account_Transaction_List = array();

$connectionInfo = array(  "Database"=>'**', "UID"=>'**', "PWD"=>'***');
$conn = sqlsrv_connect( "**", $connectionInfo );

if( $conn === false ) {
    echo "Couldn't connect to SQL Server on $this->servername.<br />";
    die( print_r( sqlsrv_errors(), true));
} else {
    echo "Connected!";
}
echo '<br/>';   

$result = sqlsrv_query($conn, $tsql, $params);
echo "<pre/> $result - ";print_r($result);
echo '<br/>';   

if ($result !== NULL) {  
    $rows = sqlsrv_has_rows( $result );
    if ($rows === true)  
        echo "\nthere are rows\n";  
    else   
        echo "\nno rows\n";  
}  

while($row = sqlsrv_fetch_array($result))
{
    echo 'IN <br/>';
}

?>

它returns:

Connected!

Resource id #3 - Resource id #3

no rows

无论我尝试什么,我都得不到数据。在过去的两天里,我尝试通过 ref 传递参数,将游标选项设置为连接和其他排列,但没有成功。

有没有人遇到过这个问题。任何指针将不胜感激。

谢谢

我注意到的唯一问题是 $creditlimit 设置为 SQLSRV_PARAM_INOUT。我建议在 运行 查询之后检查 sqlsrv_errors()。返回的警告可能会揭示一些情况。尝试这样的事情:

<?php
$bol_id        = 426863;
$account_id    = 26293;
$totalamount   = 0.00;
$storagedays   = 0;
$accountcredit = 0.00;
$creditlimit   = 0.00;

$params = [
    [$bol_id,        SQLSRV_PARAM_IN],
    [$account_id,    SQLSRV_PARAM_IN],
    [$totalamount,   SQLSRV_PARAM_OUT],
    [$storagedays,   SQLSRV_PARAM_OUT],
    [$accountcredit, SQLSRV_PARAM_OUT],
    [$creditlimit,   SQLSRV_PARAM_OUT],
];

$conn = sqlsrv_connect("**", ["Database" => "**", "UID"=> "**", "PWD" => "***"]);

if ($conn === false) {
    echo "Couldn't connect to SQL Server on $this->servername.<br />";
    die(print_r(sqlsrv_errors(), true));
} else {
    echo "Connected!<br/>";
}

$tsql = "{CALL usp_ws_s_GetCargoClearanceApproval_t(?, ?, ?, ?, ?, ?)}";  

$result = sqlsrv_query($conn, $tsql, $params);

if ($err = sqlsrv_errors()) {
    echo "There were errors or warnings!<br/>";
    print_r($err);
    echo "<br/>";
}

if ($result && sqlsrv_has_rows($result)) {
    echo "There are rows<br/>";
} else {
    echo "There are no rows<br/>";
}

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
    print_r($row);
    echo "<br/>";
}

?>

显然,sqlsrv 扩展在绘制在 sp 中计算的数据时存在问题。以前我的 sp 读 -

ALTER PROCEDURE [dbo].[usp_ws_s_GetCargoClearanceApproval_t]
@BOLID INT,
@ACCOUNTID INT,
@TOTALAMOUNT FLOAT OUT,
@STORAGEDAYS INT OUT,
@ACCOUNTCREDIT FLOAT OUT,
@CREDITLIMIT FLOAT OUT
AS

BEGIN
--SET NOCOUNT ON
    set @TOTALAMOUNT= '25.00'; SET @STORAGEDAYS = 5; SET @ACCOUNTCREDIT =2500.00;  SET @CREDITLIMIT=75000.00 ;
    select @TOTALAMOUNT,@STORAGEDAYS,@ACCOUNTCREDIT,@CREDITLIMIT;
END

为了获取数据,我必须修改 sp 才能读取

select @TOTALAMOUNT as TotalAmount,@STORAGEDAYS as StorageDays,@ACCOUNTCREDIT as AccountCredit,@CREDITLIMIT as CreditLimit;

在这种情况下,我也不必设置输出参数,大家都很高兴:)