我的php应用程序正在使用MS SQLSrv驱动程序。在我的数据库中调用存储过程时,运行sqlsrv_execute()后出现以下错误:参数计数和参数计数不匹配。我的守则如下:
$sql = "{call myStoredProcedure(?, ?, ?, ?, ?, ?)}";
//Passing by reference instead of value, otherwise sqlsrv_prepare is not happy
$params = array(array(&$param1, SQLSRV_PARAM_IN),
array(&$param2, SQLSRV_PARAM_IN),
array(&$param3, SQLSRV_PARAM_IN),
array(&$param4, SQLSRV_PARAM_IN),
array(&$param5, SQLSRV_PARAM_IN),
array(&$param6, SQLSRV_PARAM_OUT)
);
/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $sql, $params);
if( $stmt )
{
echo "Statement prepared.\n";
}
else
{
echo "Error in preparing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
//TODO: Resolve error, "param count and argument count don't match"
$stmt = sqlsrv_execute($conn, $sql, $params);
//This statement will run, but no rows are returned and rowCount is false.
//$stmt = sqlsrv_query($conn, $sql, $params);
$rowCount = sqlsrv_num_rows( $stmt );
$numFields = sqlsrv_num_fields( $stmt );
//Rest of code...
我花了一个小时就完成了这项工作,并通过PHP.Net和Microsoft文档进行了梳理。有没有其他人遇到过类似的错误?任何帮助表示赞赏。
是的,我检查了我的参数,我的存储过程需要6个参数。
更新:
存储过程代码段:
ALTER PROCEDURE [dbo].[myStoredProcedure]
@param1 VARCHAR(64),
@param2 VARCHAR(64),
@param3 DATETIME,
@param4 DATETIME,
@param5 INT = 9,
@param6 INT OUTPUT
AS
BEGIN
//Do stuff
END
答案 0 :(得分:1)
您正在使用SQLSRV_EXECUTE
incorrectely。您获得的错误实际上是PHP错误,告诉您将错误的参数传递给SQLSRV_EXECUTE
。
定义为:
sqlsrv_execute(resource $stmt)
但你打电话给:sqlsrv_execute($conn, $sql, $params)
我已经修复了下面的代码,假设存储过程设置正确。如果有效,请告诉我。
$sql = "{call myStoredProcedure(?, ?, ?, ?, ?, ?)}";
//Passing by reference instead of value, otherwise sqlsrv_prepare is not happy
$params = array(array(&$param1, SQLSRV_PARAM_IN),
array(&$param2, SQLSRV_PARAM_IN),
array(&$param3, SQLSRV_PARAM_IN),
array(&$param4, SQLSRV_PARAM_IN),
array(&$param5, SQLSRV_PARAM_IN),
array(&$param6, SQLSRV_PARAM_OUT)
);
/* Create the statement. */
$stmt = sqlsrv_prepare( $conn, $sql, $params);
if( $stmt )
{
echo "Statement prepared.\n";
}
else
{
echo "Error in preparing statement.\n";
die( print_r( sqlsrv_errors(), true) );
}
//Shouldn't assign this to $stmt, $stmt can be reused for multiple sqlsrv_execute() calls
$result = sqlsrv_execute($stmt);
if($result === false)
{
//Error handling
}
else
{
$rowCount = sqlsrv_num_rows( $result );
$numFields = sqlsrv_num_fields( $result );
}