Stored Procedures
?
$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); Used to return the value of return-103 this class directly.
Mssql_bind ($stmt, "@outvar", $b, sqlvarchar,true);//To return the output parameters defined in the stored procedure
Mssql_bind ($stmt, "@invar", $a, SQLINT4);
$result = Mssql_execute ($stmt, true);//cannot return result set, can only get output parameters
$result = Mssql_execute ($stmt, false); return result set
$records =mssql_fetch_array ($result);
Print_r ($records);
Mssql_next_result ($result); next result set, the next one is the output parameter when equal to False
Echo $b;
Echo $val;
?>
Here are some other places to see.
Small trouble:
We have followed the usual practice of using a MS SQL Server stored procedure Proca, which gives an output parameter Nreturn,
and returns a result set.
We had a little trouble getting PHP to call this proca.
Fish and paws can not be both:
We had hoped that such code would be able to get both the output parameter and the result set returned:
Initialize 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);
Binding 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);
The result set is obtained, however, so that the $nreturn parameter is not taken out of the output parameter.
If the last sentence should read:
$db _mssql->query_id = Mssql_execute ($stmt, true);
The output parameter is got, and the result set is gone.
It seems to be a fish and bear cake can not be the same.
Can't php even do this? The problem is not in the PHP manual either.
Explanation from the PHP maintainer:
Originally we this call method is the PHP 4.3 version must be supported before.
"But since the PHP 4.3 version," They say, "PHP changes this feature in order to be able to return multiple result sets to a compatible stored procedure." ”
"If you don't need a result set, you should set Mssql_execute's second optional argument to true so that you can get the output parameters after the Mssql_execute method." ”
"If you need to return the result set, you should call a mssql_next_result for each result set." After the last result set returns, you will get the return value false when you call Mssql_next_result, and then you can access the output parameters. ”
Solve:
In the end we fill in a sentence:
After the last result has been returned, the return value would have the value returned by the stored procedure.
Mssql_next_result ($db _mssql->query_id);
Immediately, the magic is in effect:
PHP fills in the correct output parameters into the $nretval.