Home > Backend Development > PHP Tutorial > How to get the output parameters of mssql stored procedure in php

How to get the output parameters of mssql stored procedure in php

WBOY
Release: 2016-07-25 08:59:09
Original
1057 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

Problem: As usual, a stored procedure procA of MS Sql Server is used, which gives an output parameter nReturn and returns a result set. There are some small problems in how to let PHP call this procA. I originally hoped that such code could get both the output parameters and the returned result set:

  1. // Initializes a 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 storage Process, get the QueryID:
  13. $db_mssql->Query_ID = mssql_execute($stmt,false);
  14. ?>
Copy the code

Although the result set is obtained, the $nReturn parameter cannot be obtained in this way Output parameters.

If you change the last sentence to:

  1. $db_mssql->Query_ID = mssql_execute($stmt,true);
Copy the code

I got the output parameters, but the result set is gone.

Solution: At the end we add a sentence:

  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 works: 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