PHP gets the output parameter function implementation of mssql stored procedure

  • 2020-05-26 08:05:30
  • OfStack

In the process of development, you may encounter that you cannot get the output parameters of MSSQL stored procedure. Many friends do not know what to do. This article will introduce the function realization of PHP to get the output parameters of mssql stored procedure in detail
 
<? 
$conn=mssql_connect("127.0.0.1","user","passwd"); 
mssql_select_db("mydb"); 
$stmt=mssql_init("pr_name",$conn);// 
$a=50001; 
mssql_bind($stmt,"RETVAL",$val,SQLVARCHAR); // For direct return return -103 The value of this class.  
mssql_bind($stmt,"@outvar",$b,SQLVARCHAR,true);// Used to return output parameters defined in the stored procedure  
mssql_bind($stmt,"@invar",$a,SQLINT4); 
$result = mssql_execute($stmt,true);// You cannot return a result set, only output parameters  
//$result = mssql_execute($stmt,false); // Return result set  
//$records=mssql_fetch_array($result); 
//print_r($records); 
//mssql_next_result($result); Under the 1 All of the result sets, when theta is equal to theta FALSE When the 1 These are the output parameters  
echo $b; 
echo $val; 
?> 

Here's what you see from somewhere else.
Little trouble:
We routinely used an MS Sql Server stored procedure, procA, which gives an output parameter, nReturn,
And it returns a result set.
We had a little trouble getting PHP to call this procA.
You can't have your cake and eat it:
We had hoped that this code would get both output parameters and the returned result set:
 
//  Initializes the parameters to be passed into the stored procedure : 
$nYear = 2004; 
$nPageSize = 20; 
$nPageNo = 1; 
// Initializes a stored procedure: 
$stmt = mssql_init("proc_stat_page", $db_mssql->Link_ID); 
//  Bind input parameters:  
mssql_bind($stmt, "@nReturn", $nReturn, SQLINT4, TRUE); 
mssql_bind($stmt, "@nYear", $nYear, SQLINT4); 
mssql_bind($stmt, "@nPageSize", $nPageSize, SQLINT4); 
mssql_bind($stmt, "@nPageNo", $nPageNo, SQLINT4); 
//  Execute the stored procedure and get QueryID :  
$db_mssql->Query_ID = mssql_execute($stmt,false); 

Although the result set is obtained, the $nReturn parameter does not get the output parameter.
If you change the last sentence to:
$db_mssql- > Query_ID = mssql_execute($stmt,true);
The output parameters are there, and the result set is gone.
It's like you can't have your cake and eat it.
Can't PHP even do that? The PHP manual doesn't cover this either.
From the maintainer of PHP:
This call method was originally supported by PHP 4.3.
"However, since version 4.3 of PHP," they said, "PHP has changed this feature to be compatible with stored procedures returning multiple result sets."

"If you do not need a result set, you should set the second optional parameter of mssql_execute to TRUE, so that after the mssql_execute method you can get the output parameter."

"If you need to return result sets, you should call mssql_next_result once for each result set. After the last result set is returned, you call mssql_next_result and you get the return value FALSE, at which point you can access the output parameters."
Solution:
Let's add a sentence at the end:
// After the last result has been returned the return value will have the value returned by the stored procedure.
mssql_next_result($db_mssql- > Query_ID);
Immediately, the magic worked:
PHP fills the correct output parameter into $nRetVal.

Related articles: