首先安裝擴展
windows
分為兩個步驟
- 找到對應自己PHP版本的pdo擴展,下載解壓出來,并且在php.ini里面啟用擴展,需要注意的問題是php版本以及是否為安全版本
- 下載 ODBC Driver https://docs.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017,這個沒啥注意的,你是啥系統(tǒng)就下載啥安裝包就行
linux 和 windows差不多,安裝擴展的話直接可以用pecl
當你成功加載了可以在phpinfo()里面看到,當然了,如果你安裝擴展這些都有諸多問題都話,~你可真拉稀。
thinkphp操作sqlsrv儲存過程
我使用的tp版本是5.0和操作多個數(shù)據(jù)庫,希望能對你有所幫助
配置config文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
// 賬號數(shù)據(jù)庫 'UserDBConn' => [ 'type' => 'sqlsrv' , // 服務器地址 'hostname' => '139.129.1.1' , // 數(shù)據(jù)庫名 'database' => 'DB3' , // 用戶名 'username' => 'xxxx' , // 密碼 'password' => 'tt123!@#' , // 端口 'hostport' => '5188' ], // 金幣數(shù)據(jù)庫 'ScoreDBConn' => [ 'type' => 'sqlsrv' , // 服務器地址 'hostname' => '139.129.1.1' , // 數(shù)據(jù)庫名 'database' => 'DB2' , // 用戶名 'username' => 'xxxx' , // 密碼 'password' => 'tt123!@#' , // 端口 'hostport' => '5188' ], // 記錄數(shù)據(jù)庫 'RecordDBConn' => [ 'type' => 'sqlsrv' , // 服務器地址 'hostname' => '139.129.1.1' , // 數(shù)據(jù)庫名 'database' => 'DB1' , // 用戶名 'username' => 'xxxx' , // 密碼 'password' => 'tt123!@#' , // 端口 'hostport' => '5188' ], |
修改thinkphp/library/think/Model.php
在末尾追加
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * @param $DbconnName */ protected function Dbconn( $DbconnName ){ try { $conn = Db::connect( $DbconnName ); } catch (\InvalidArgumentException $e ){ echo '連接異常' ; die ; } return $conn ; } |
添加模型
Agent.php
查詢和增刪改都可以調(diào)用query,如果你沒有想要獲取的結果集的話可以調(diào)用execute()。
query()有一個弊端,如果你的綁定參數(shù)的形式(非參數(shù)綁定)是直接寫進sql的話,他有可能會判斷你這個不是一個儲存過程;
具體實現(xiàn)請查看thinkphp/library/think/db/Connection.php:368行,當然也不會有結果集返回。
你也可以用調(diào)用procedure(),這個方法調(diào)用的話就一定會返回結果集。
起初我就是這個問題,并沒有采用綁定參數(shù)的形式提交,直接寫sql,就獲取不到結果集,后來我在我的sql提行里面加入了SET NOCOUNT ON;,才能勉強拿到返回,在文章最后我給出了我最開始獲取的結果集的方案例子,但是真的拉稀,你們可以看看,不要吐槽。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Agent extends Model { public $Dbname = 'UserDBConn' ; public function GetIndirectAgentList( $agentId , $strAccount , $strSuperior , $iPageIndex , $pagesize ) { $conn = $this ->Dbconn( $this ->Dbname); try { $TotalCount = 0; $res = $conn ::query( 'exec [dbo].[Agent_GetAgentList] :agentId,:strAccount,:strSuperior,:iPageIndex,:pagesize,:TotalCount' , [ 'agentId' => $agentId , 'strAccount' => [ $strAccount , PDO::PARAM_STR], 'strSuperior' => [ $strSuperior , PDO::PARAM_STR], 'iPageIndex' => [ $iPageIndex , PDO::PARAM_INT], 'pagesize' => [ $pagesize , PDO::PARAM_INT], 'TotalCount' => [ $TotalCount , PDO::PARAM_INPUT_OUTPUT], ]); } catch (PDOException $e ) { return false; } return $res ; } } |
最初的Agent.php
很顯然 這里并不會獲取到@AgentID 以及 @TotalCount;他只會返回Agent_GetAgentList的結果集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public function GetIndirectAgentList( $agentId , $strAccount , $strSuperior , $iPageIndex , $pagesize ) { $conn = $this ->Dbconn( $this ->Dbname); try { $res = $conn ->query(' SET NOCOUNT ON; declare @AgentID int; declare @TotalCount int; exec [dbo].[Agent_GetAgentList] '.$agentId.' ,\ '' . $strAccount . '\',\'' . $strSuperior . '\',' . $iPageIndex . ',' . $pagesize .',@TotalCount output; select @AgentID as AgentID,@TotalCount as TotalCount '); } catch (PDOException $e ) { return false; } return $res ; } |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_42848765/article/details/103859844