PHP / sqlsrv_query资源ID为3但不返回数据

时间:2017-10-17 16:28:10

标签: php sql-server sqlsrv

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

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/>';
}

?>

它返回:

  

连接!

     

资源ID#3 - 资源ID#3

     

没有行

无论我尝试什么,我都无法获取数据。我试过通过引用传递params,将光标选项设置为连接和过去两天的其他排列,没有运气。

有没有人遇到过这个问题。任何指针都将受到赞赏。

由于

2 个答案:

答案 0 :(得分:2)

我注意到的唯一问题是$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/>";
}

?>

答案 1 :(得分:0)

显然,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;

在这种情况下我也没有设置输出参数,每个人都很高兴:)