How does PHP obtain the output parameters of the MSSQL Stored Procedure?

Source: Internet
Author: User
$ 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 values of the return-103 class directly. Mssql_bind ($ stmt, "@ outvar", $ B, sqlvarchar, true); // return the output parameter 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 parameter 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.CodeYou can get both the output parameters and the returned result set:
// 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:
At the end, let's add one sentence:
// 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.