PHP gets the output parameters of MSSQL stored procedure to implement _php tutorial

Source: Internet
Author: User
In the development process may encounter unable to obtain the MSSQL stored procedures output parameters, many friends do not know what to do, this article will be detailed introduction of PHP to get MSSQL of the stored procedure output parameters of the implementation, need to understand the friend can refer to the next
Copy CodeThe code is as follows:
$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); The value that is used to directly return return-103 this class.
Mssql_bind ($stmt, "@outvar", $b, sqlvarchar,true);//used to return an output parameter defined in a stored procedure
Mssql_bind ($stmt, "@invar", $a, SQLINT4);
$result = Mssql_execute ($stmt, true);//cannot return result set, can only get output parameter
$result = Mssql_execute ($stmt, false); return result set
$records =mssql_fetch_array ($result);
Print_r ($records);
Mssql_next_result ($result); next result set, when equal to false the next one is the output parameter
Echo $b;
Echo $val;
?>

These are seen from somewhere else.
Little Trouble
We routinely use a MS SQL Server stored procedure Proca, which gives an output parameter of Nreturn,
and returns a result set.
We had a little bit of a problem getting PHP to call this proca.
You can't have your cake and eat it both:
We would have liked the code to get both the output parameters and the result set returned:
Copy CodeThe code is as follows:
Initialize the parameters that will 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);

Although the result set is obtained, this $nreturn parameter is not available for the output parameter.
If the last sentence should read:
$db _mssql->query_id = Mssql_execute ($stmt, true);
The output parameters have been obtained, and the result set is gone.
It seems to be a fish and bear cake can not be both.
Can't php even do this? This is not a problem in the PHP manual.
Explanations from the PHP maintainer:
Originally we call this method is the PHP 4.3 version is definitely supported before.
"But since php 4.3," They said, "PHP has changed this feature in order to be able to return multiple result sets for a compatible stored procedure. ”

"If you don't need a result set, you should set the second optional parameter of Mssql_execute 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 Mssql_next_result for each result set." After the last result set is returned, you will get the return value false when you call Mssql_next_result again, and you will be able to access the output parameters. ”
Solve:
in the end, we make up 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 came into effect:
PHP fills in the correct output parameters into the $nretval.

http://www.bkjia.com/PHPjc/326279.html www.bkjia.com true http://www.bkjia.com/PHPjc/326279.html techarticle in the development process may encounter the inability to obtain the MSSQL stored procedures output parameters, many friends do not know what to do, this article will detail the PHP get MSSQL stored procedure output parameters ...

  • 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.