0

I try to used PHP to Connect to SQL Server to call a procedure in it. But there no result return back. Please give me some advise.

Example Stored Procedure:

ALTER  PROCEDURE [MemberJoin]
    @username varchar(25),
    @password varchar(50),
AS
If (EXISTS(SELECT username FROM tbl_user WHERE username = @username ))
    Begin
        RETURN -2
    End
BEGIN TRANSACTION
BEGIN
   INSERT INTO tbl_user (username , password ) VALUES (@username, @password)
    If (@@ERROR <> 0 OR @@ROWCOUNT = 0)
    Begin
        ROLLBACK TRANSACTION
        RETURN -3
    End
COMMIT TRANSACTION
END

RETURN 1

PHP Code

$return = null;

$server = "LOCALHOST\\SQLEXPRESS";
$connectionInfo = array( "Database"=>"SHOP", "UID"=>"sa", "PWD"=>"");
$Conn = sqlsrv_connect( $server, $connectionInfo);

$sql = "{call ? = MemberJoin(@username=?,@password=?)}";

$param = [
    [&$return, SQLSRV_PARAM_OUT],
    ['testingsr', SQLSRV_PARAM_IN],
    ['12345667', SQLSRV_PARAM_IN],
];

$stmt = sqlsrv_query ( $Conn , $sql, $param );

echo($return);

Result null with no error. I expect that return values to be 1,-2 or -3

Thank you in advance.

2
  • Have you tried [&$return, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_INT, SQLSRV_SQLTYPE_INT]? Ref: sqlsrv_prepare Commented Aug 22, 2020 at 6:26
  • This answer may help (use the code without PDO). Commented Aug 22, 2020 at 16:39

1 Answer 1

1

You need to declare a variable to capture the return value and then check the value of the variable. Below is an example.

DECLARE @return_status int;  
EXEC @return_status = dbo.sproc_checkstate;  
SELECT 'Return Status' = @return_status;

For what you are doing it may be easier to return a column. For example you can change RETURN -2 to SELECT -2 as return_status and return the value as a return_status column in a resultset.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I'd like to change the procedure code too. But I can't change the SQL code. :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.