我正在尝试使用Microsoft的SQLSRV驱动程序在PHP中调用过程。这是我的存储过程,调用函数。但它向我展示了一个错误:
"执行语句时出错。数组 ( [0] => 数组 ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -14 [代码] => -14 [2] => 无效 参数已传递给sqlsrv_query。[消息] => 无效 参数已传递给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));
}
}
好吧,sqlsrv_query
期望作为函数的第一个参数资源sqlsrv_connect
但您正在传递一些神秘类db
的实例。也许你应该使用变量$cid
而不是$db
($db
和$limit
在你的函数中似乎没有必要)。
$sql = sqlsrv_query($cid, $callSP, $params);