使用 sqlsrv 驱动程序调用 PHP 中的存储过程
Call stored procedure in PHP using sqlsrv driver
我正在尝试使用 Microsoft 的 SQLSRV 驱动程序调用 PHP 中的过程。这是我的存储过程,调用函数。但它显示了一个错误:
"Error in executing statement. Array ( [0] => Array ( [0] => IMSSP
[SQLSTATE] => IMSSP [1] => -14 [code] => -14 [2] => An invalid
parameter was passed to sqlsrv_query. [message] => An invalid
parameter was passed to 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
函数的第一个参数资源,但您正在传递一些神秘的实例 class db
。也许你应该使用变量 $cid
而不是 $db
($db
和 $limit
在你的函数中似乎是不必要的)。
$sql = sqlsrv_query($cid, $callSP, $params);
我正在尝试使用 Microsoft 的 SQLSRV 驱动程序调用 PHP 中的过程。这是我的存储过程,调用函数。但它显示了一个错误:
"Error in executing statement. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -14 [code] => -14 [2] => An invalid parameter was passed to sqlsrv_query. [message] => An invalid parameter was passed to 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
函数的第一个参数资源,但您正在传递一些神秘的实例 class db
。也许你应该使用变量 $cid
而不是 $db
($db
和 $limit
在你的函数中似乎是不必要的)。
$sql = sqlsrv_query($cid, $callSP, $params);