-
-
- $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); //Used to directly return values such as return -103.
- mssql_bind($stmt,"@outvar",$b,SQLVARCHAR,true);//Used to return the output parameters defined in the stored procedure
- mssql_bind($stmt,"@invar",$a,SQLINT4);
- $result = mssql_execute($stmt,true);//Cannot return the result set, you can only get the output parameters
- //$result = mssql_execute($stmt,false); //Return the result set
- //$records=mssql_fetch_array( $result);
- //print_r($records);
- //mssql_next_result($result); The next result set, when equal to FALSE, the next one is the output parameter
- echo $b;
- echo $val;
- ?> ;
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:
- // Initializes a stored procedure:
- $nYear = 2004;
- $nPageSize = 20;
- $nPageNo = 1;
- // Initializes a stored procedure:
- $stmt = mssql_init("proc_stat_page", $db_mssql->Link_ID);
- // Bind 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 storage Process, get the QueryID:
- $db_mssql->Query_ID = mssql_execute($stmt,false);
- ?>
-
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:
-
- $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:
-
- // 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);
-
Copy code
immediately, The magic works:
PHP populates $nRetVal with the correct output parameters.
|