PHP: Param count and argument count don't match
My php application is using the MS SQLSrv driver. When making a call to a stored procedure in my database, I get the following error after running sqlsrv_execute(): param count and argument count do开发者_StackOverflown't match. My Code is as follows:
$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...
I've spent an hour on this and combed through PHP.Net and Microsoft documentation. Has anyone else encountered a similar error? Any help is appreciated.
And yes, I've checked my param count, my stored procedure takes 6 parameters.
UPDATE:
Stored procedure snippet:
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
You are using SQLSRV_EXECUTE
incorrectely. The error that your are getting is actually a PHP error, telling you that you are passing the wrong parameters to SQLSRV_EXECUTE
.
Defined as:
sqlsrv_execute(resource $stmt)
But you are calling: sqlsrv_execute($conn, $sql, $params)
I've fixed up the code below, assuming the stored procedure is setup properly. Let me know if it works.
$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 );
}
精彩评论