search
Homephp教程php手册PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解

PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解

Jun 13, 2016 am 11:43 AM
mssqloutputphpserverIncludeparameterstorageofresultObtainDetailed explanationtransferprocess

【PHP Source Code】:

复制代码 代码如下:


$dbh = new PDO('sqlsrv:server=连接地址;Database=数据库名', 用户名, 密码);
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();

 //获取第一个结果集.
 $rowset_1 = $stmt->fetch(PDO::FETCH_ASSOC);
 print_r($rowset_1);
 echo '

';

 //获取第二个结果集.
 $stmt->nextRowset();
 $rowset_2 = $stmt->fetch();
 print_r($rowset_2);
 echo '

';
 $stmt->nextRowset();
 // 获取两个输出类型的参数
 echo $nReturnValue.'

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


【SQL PROCEDURE】:

复制代码 代码如下:


/**
* 用于测试PDO调用MsSQLServer2012存储过程获取复合结果集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


【一些要注意的问题】:当bindParam中存在需要输出类型的参数时,必须包含长度($length)。
【备注】:$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.
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools