Jump to content

Calling a MSSQL Stored Procedure with multiple inputs and outputs


Recommended Posts

    $myPMSource = "FANESTRA5";   
    $period = -8;
    
    $myDB = "dog";
    $myCLSource = "cat";

    $sql = "{CALL retrieveproductiondata(?,?)}";
    $params = array(array($myPMSource, SQLSRV_PARAM_IN, $period, SQLSRV_PARAM_IN), array($myDB, SQLSRV_PARAM_INOUT, $myCLSource, SQLSRV_PARAM_INOUT));
    $stmt = sqlsrv_query($aconn, $sql, $params);

 

All of the web examples show and work with just one parameter of each. I have not been able to find the proper syntax for multiples.

Adding more what?

This is an error msg  i get: In read_production_data
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -16

 => -16 [2] => An invalid PHP type for parameter 1 was specified. [message] => An invalid PHP type for parameter 1 was specified. ) )

He meant, did you try adding more parameters to the array.  You have a basic misunderstanding of the manual:  https://www.php.net/manual/en/function.sqlsrv-query.php

"params" is an array of arrays, where each array element represents one, in, out or in/out parameter.  Your code seems to imply that you think there are separate arrays for input and output.  That is not the case.  There is only one array, and you need a child array for each parameter (in order of declaration for the sproc).

Since you did not provide us the source of the sproc, we don't know how it is defined.  Just trying to intuit what you are doing from your sample code, this looks more likely to work:

 

$params = array(
    array($myPMSource, SQLSRV_PARAM_IN),
    array($period, SQLSRV_PARAM_IN), 
    array($myDB, SQLSRV_PARAM_INOUT), 
    array($myCLSource, SQLSRV_PARAM_INOUT)
);

This style requires that the sql server driver assume the php and mssql server data types from a preset of the variables, but there are optional parameters (3 and 4) that can be used with constants documented on the query page, you could pass to explicitly tell the driver what is expected, within each single parameter array.

I haven't done any PHP with mssql in a long time, but it is possible that for any output variables you may need to pass those by reference.  That at least is what the current microsoft documentation shows.  So you might need something like this:

$params = array(
    array($myPMSource, SQLSRV_PARAM_IN),
    array($period, SQLSRV_PARAM_IN), 
    array(&$myDB, SQLSRV_PARAM_INOUT), 
    array(&$myCLSource, SQLSRV_PARAM_INOUT)
);

Last but not least, mssql does have a PDO driver, and that might be simpler and easier to use, as you would simply bind each parameter prior to the call.  See: https://learn.microsoft.com/en-us/sql/connect/php/pdo-sqlsrv-driver-reference?view=sql-server-ver16

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.