Home > Backend Development > PHP Tutorial > Detailed explanation of PHP calling MsSQL Server 2012 stored procedure to obtain multiple result sets (including output parameters)_PHP tutorial

Detailed explanation of PHP calling MsSQL Server 2012 stored procedure to obtain multiple result sets (including output parameters)_PHP tutorial

WBOY
Release: 2016-07-21 15:01:08
Original
928 people have browsed it

【PHP Source Code】:

Copy code The code is as follows:

$dbh = new PDO( 'sqlsrv:server=connection address;Database=database name', username, password);
try {
$procName = "P_Test_GetMixData";
$stmt = $dbh->prepare("EXEC $procName ?, ?, ?");
$nReturnValue = 0;
$strReturnValue = "";
$strSearchValue = "abandonship";
$stmt->bindParam(1, $ nReturnValue, PDO::PARAM_INT | PDO::PARAM_INPUT_OUTPUT, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE);
$stmt->bindParam(2, $strReturnValue, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 10);
$ stmt->bindParam(3, $strSearchValue , PDO::PARAM_STR);
$stmt->execute();

//Get the first result set.
$rowset_1 = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($rowset_1);
echo '

';

//Get the second result Set.
$stmt->nextRowset();
$rowset_2 = $stmt->fetch();
print_r($rowset_2);
echo '
$stmt->nextRowset();
// Get parameters of two output types
echo $nReturnValue.'

';
echo $ strReturnValue;
} catch (Exception $e) {
echo $e->getMessage();
}

[SQL PROCEDURE]:
Copy code The code is as follows:

/**
* Used to test PDO calling MsSQLServer2012 stored procedure to obtain composite result set Demo
* Code CreateBy abandonship 2012.10.11
**/
CREATE PROCEDURE [dbo].[ P_Test_GetMixData](
@Message_1 tinyint output,
@Messgae_2 varchar(10) output,
@SearchValue varchar(50)
) As
set nocount on

set @ Message_1 = 123
set @Messgae_2 = 'Hi,there!This is abandonship!'
select * from _T1 where col1 like '%'+@SearchValue+'%'
select * from _T2 where col1 like ' %'+@SearchValue+'%'
set nocount off

[Some issues to note]: When bindParam contains parameters that require output type, the length ($ length).
【Remarks】:$length: An optional (integer) length of the data type. You can specify PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE to indicate the default size when using PDO::PARAM_INT or PDO ::PARAM_BOOL in $data_type.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328018.htmlTechArticle[PHP Source Code]: Copy the code as follows: $dbh = new PDO('sqlsrv:server=Connection address ;Database=database name', username, password); try { $procName = "P_Test_GetMixData"; $s...
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