0

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 don'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
4
  • Can you post some information on the actual stored procedure? Jun 29, 2011 at 21:01
  • Wild guess, because I'm far from being an expert, but since the last param is an output parameter, I don't see why it should be bound: the stored proc won't do anything with the given value.
    – JB Nizet
    Jun 29, 2011 at 21:03
  • Unfortunately, from what I've read on Microsoft's docs, you have to provide all parameters, input, output and both bi-directional in/out. Jun 29, 2011 at 21:07
  • 2
    I'm no expert either, but in the example here ( msdn.microsoft.com/en-us/library/cc296181.aspx ), the parameters are passed to sqlsrv_prepare, and the return value ($stmt) is then executed (in your code, you execute using $sql and $params again). Oversight or by design?
    – horatio
    Jun 29, 2011 at 21:12

1 Answer 1

1

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 );
}
2
  • Also, note the change "$result = sqlsrv_execute($stmt);" Part of the point of the prepare / execute strategy here is that you can change the variables assigned above and just execute it again for a new result.
    – CLo
    Sep 24, 2011 at 5:20
  • Thank your for this answer. I was struggling for over an hour to get prepared statements to work. PS: Make sure the DB User has "EXECUTE" privileges on th stored procedure ;)
    – Daywalker
    Jul 3, 2018 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.