How does PHP get the output parameters of the MSSQL stored procedure?

Source: Internet
Author: User
Tags mssql stmt
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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.