使用sqlsrv驱动程序在PHP中调用存储过程

时间:2015-04-28 11:01:04

标签: php sqlsrv

我尝试使用Microsoft的SQLSRV驱动程序在PHP中调用过程。这是我的存储过程,调用函数。但它显示了一个错误:

  

"执行语句时出错。数组([0] =>数组([0] => IMSSP   [SQLSTATE] => IMSSP [1] => -14 [code] => -14 [2] =>无效   参数传递给sqlsrv_query。 [message] =>无效   参数传递给sqlsrv_query。 ))"

存储过程:

ALTER procedure [dbo].[getRelatedProductById]
    (@productId int)
AS
BEGIN
    declare
        @id varchar(max),
        @sql nvarchar(max)

BEGIN TRY
    select @id = relatedProduct   
    from Product 
    where id = @productId

    set @sql = 'select * from Product where id in(' + @id + ')' +'and id != '+ Convert(varchar, @productId)
    exec sp_executesql @sql
END TRY
BEGIN CATCH 
    SELECT 
        ERROR_NUMBER() AS ErrorNumber, 
        ERROR_SEVERITY() AS ErrorSeverity, 
        ERROR_STATE() AS ErrorState, 
        ERROR_PROCEDURE() AS ErrorProcedure, 
        ERROR_LINE() AS ErrorLine, 
        ERROR_MESSAGE() AS ErrorMessage;

    select @@error
    print @@error
END CATCH
END

-- exec getRelatedProductById 1

PHP函数:

public function getRelatedProduct($cid,$productId,$limit) {
    $db=new db();
    settype($productId, "integer");
    $params = array(array($productId, SQLSRV_PARAM_IN));
    $callSP = "{CALL getRelatedProductById(?)}";
    $sql=sqlsrv_query($db, $callSP,$params);
    if( $sql === false )
        {
             echo "Error in executing statement.\n";
             die( print_r( sqlsrv_errors(), true));
        }
}

1 个答案:

答案 0 :(得分:1)

好吧,sqlsrv_query期望从sqlsrv_connect函数作为第一个参数资源,但是你传递了一些神秘类db的实例。也许您应该使用变量$cid代替$db$db并且$limit在您的函数中似乎没有必要。)

$sql = sqlsrv_query($cid, $callSP, $params);