Search code examples
phpsql-server

How can I bind a parameter in sqlsrv_prepare?


We have this stored procedure which created by the other guiys

    DECLARE @RC int
DECLARE @Number varchar(10)

-- TODO: Set parameter values here.

EXECUTE @RC = [dbo].[spWebGetPrice] 
   @Number
GO

My question is how can I bind a value on @Number using sqlsrv_prepare.

I am using this code but it says that the parameter is still not supplied

$sql = sqlsrv_prepare($conn,"spWebGetPrice", ['@Number', 123456]);

Solution

  • Procedure arguments are separate from prepared query placeholders.

    sqlsrv_prepare expects as its second argument a full string of SQL, with ? in place of data which will be supplied as separate parameters. This is easier to illustrate with a plain select query:

    sqlsrv_prepare($conn, 'Select * From Users Where Username=?', [$username]);
    

    To run a stored procedure, you need to do the same thing - write out the SQL with ? placeholders:

    sqlsrv_prepare($conn, 'Exec spWebGetPrice @Number=?', [123456]);