PHP調用MsSQL Server 2012存儲過程獲取多結果集(包含output參數)的詳解_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:Smarty foreach控制循環次數的實現詳解在smarty中數組是經常會用到的,循環遍歷數組用 section 或者 foreach ,如何得到數組長度或者判斷一個數組個數呢 1.可以用{$array| count} 來試試。 2. 復制代碼 代碼如下: {foreach from=$variable key=key name=name iteam=value} {$variable|@count} // 獲取數組長
本篇文章是對PHP調用MsSQL Server 2012存儲過程獲取多結果集(包含output參數)的方法進行了詳細的分析介紹,需要的朋友參考下【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 '<br><br>';
//獲取第二個結果集.
$stmt->nextRowset();
$rowset_2 = $stmt->fetch();
print_r($rowset_2);
echo '<br><br>';
$stmt->nextRowset();
// 獲取兩個輸出類型的參數
echo $nReturnValue.'<br><br>';
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.
分享:PHP中使用協同程序實現合作多任務第1/2頁PHP5.5一個比較好的新功能是實現對生成器和協同程序的支持。對于生成器,PHP的文檔和各種其他的博客文章(就像這一個或這一個)已經有了非常詳細的講解。協同程序相對受到的關注就少了,所以協同程序雖然有很強大的功能但也很難被知曉,解釋起來也比較困難。 這篇文章
相關PHP教程:
- 相關鏈接:
- 教程說明:
PHP教程-PHP調用MsSQL Server 2012存儲過程獲取多結果集(包含output參數)的詳解
。