PHP gets the code of output parameters of mssql stored procedure

WBOY
Release: 2016-07-25 09:03:45
Original
829 people have browsed it
  1. $conn=mssql_connect("127.0.0.1","user","passwd");
  2. mssql_select_db("mydb");
  3. $stmt=mssql_init("pr_name",$conn) ;//
  4. $a=50001;
  5. mssql_bind($stmt,"RETVAL",$val,SQLVARCHAR); //Used to directly return values ​​such as return -103.
  6. mssql_bind($stmt,"@outvar",$b,SQLVARCHAR,true);//Used to return the output parameters defined in the stored procedure
  7. mssql_bind($stmt,"@invar",$a,SQLINT4);
  8. $result = mssql_execute($stmt,true);//Cannot return the result set, you can only get the output parameters
  9. //$result = mssql_execute($stmt,false); //Return the result set
  10. //$records=mssql_fetch_array( $result);
  11. //print_r($records);
  12. //mssql_next_result($result);The next result set, when equal to FALSE, the next one is the output parameter
  13. echo $b;
  14. echo $val;
  15. ?> ;
Copy code

The following are what I saw elsewhere. Little trouble: As usual, we use a stored procedure procA of MS Sql Server, which gives an output parameter nReturn, And a result set is returned. We encountered a little trouble when asking PHP to call this procA. You can’t have your cake and eat it too: I originally hoped that such code could get both the output parameters and the returned result set:

  1. // Initialize the parameters to be passed into the stored procedure:
  2. $nYear = 2004;
  3. $nPageSize = 20;
  4. $nPageNo = 1;
  5. // Initializes a stored procedure:
  6. $stmt = mssql_init("proc_stat_page", $db_mssql->Link_ID);
  7. // Bind input parameters:
  8. mssql_bind($stmt, " @nReturn", $nReturn, SQLINT4, TRUE);
  9. mssql_bind($stmt, "@nYear", $nYear, SQLINT4);
  10. mssql_bind($stmt, "@nPageSize", $nPageSize, SQLINT4);
  11. mssql_bind($ stmt, "@nPageNo", $nPageNo, SQLINT4);
  12. //Execute the stored procedure and get the QueryID:
  13. $db_mssql->Query_ID = mssql_execute($stmt,false);
Copy the code

Although I got Result set, however, the $nReturn parameter cannot get the output parameters. If you change the last sentence to: $db_mssql->Query_ID = mssql_execute($stmt,true); The output parameters are obtained, but the result set is gone. It seems like you can't have your cake and eat it too. Can't PHP even do this? This issue is not mentioned in the PHP manual.

Explanation from the PHP maintainer: Originally, our calling method was definitely supported before PHP 4.3. "However, since PHP version 4.3," they said, "in order to be compatible with stored procedures returning multiple result sets, PHP has changed this feature."

"If you don't need the 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 result sets, you should call mssql_next_result once for each result set. After the last result set is returned, you will get the return value FALSE by calling mssql_next_result. At this time, you can access the output parameters ” solve: Add a sentence at the end:

  1. // After the last result has been returned the return value will have the value returned by the stored procedure.
  2. mssql_next_result($db_mssql->Query_ID);
Copy code

Immediately, the magic took effect: PHP populates $nRetVal with the correct output parameters.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template