PHP: get the output parameter function of mssql stored procedure implementation _ PHP Tutorial

Source: Internet
Author: User
PHP obtains the output parameter function of the mssql stored procedure. In the development process, the output parameters of the MSSQL stored procedure may not be obtained. many friends do not know what to do, this article will introduce in detail the output parameters of the mssql stored procedure obtained by PHP during development, and may encounter output parameters that cannot be obtained in the MSSQL stored procedure. many friends do not know what to do, this article will introduce in detail how to implement the output parameter function of mssql stored procedure in PHP. For more information, see

The 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); // return the value of the return-103 class directly.
Mssql_bind ($ stmt, "@ outvar", $ B, SQLVARCHAR, true); // return the output parameters defined in the stored procedure
Mssql_bind ($ stmt, "@ invar", $ a, SQLINT4 );
$ Result = mssql_execute ($ stmt, true); // The result set cannot be returned and only output parameters can be obtained.
// $ Result = mssql_execute ($ stmt, false); // return result set
// $ Records = mssql_fetch_array ($ result );
// Print_r ($ records );
// Mssql_next_result ($ result); next result set. when the value is FALSE, the next result is the output parameter.
Echo $ B;
Echo $ val;
?>


The following are from other places.
Little Trouble:
We use the stored procedure procA of MS SQL Server by convention, which provides an output parameter nReturn,
A result set is returned.
We encountered a little trouble when we asked PHP to call this procA.
Fish and bear's paw cannot have both:
We wanted this code to get both the output parameters and the returned result set:

The code is as follows:


// 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 );
// Bind the 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 obtain the QueryID:
$ Db_mssql-> Query_ID = mssql_execute ($ stmt, false );


Although the result set is obtained, the $ nReturn parameter cannot obtain the output parameter.
If you change the last sentence:
$ Db_mssql-> Query_ID = mssql_execute ($ stmt, true );
The output parameter is obtained, and the result set is missing.
It seems like a fish and a bear's paw cannot have both sides.
Can't PHP even do this? This issue is not discussed in the PHP Manual.
Explanation from PHP maintainers:
Originally, this call method was certainly supported by PHP 4.3.
"However, since PHP version 4.3," they said, "to be compatible with stored procedures and return multiple result sets, PHP has changed this feature ."

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

"If you want to return the result set, you should call mssql_next_result once for each result set. After the last result set is returned, you can call mssql_next_result to obtain the returned value "FALSE". In this case, you can access the output parameters ."
Solution:
Let's add one 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, Magic takes effect:
PHP fills in the correct output parameters to $ 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.