PHP基于PDO調(diào)用sqlserver存儲過程通用方法【基于Yii框架】
本文實例講述了PHP基于PDO調(diào)用sqlserver存儲過程的方法。分享給大家供大家參考,具體如下:
由于業(yè)務(wù)這邊存儲過程一直在sqlserver上面,所以要用php去調(diào)用它,然而我們本地的是windows,而線上又是linux,一開始使用Yii框架的一些機制去調(diào)用發(fā)現(xiàn)在本地一直都是好的然而到線上就不行了,找了很多方案,最后找到了pdo這種方案,而本地使用的驅(qū)動是sqlsrv線上是dblib所以需要注意下鏈接pdo時的驅(qū)動形式,在取結(jié)果集的時候注意windows和linux好像有所不同,在我加上set nocount on后win若果直接取結(jié)果就可以拿到最后的,然而放到linux就沒了,氣死人的說,索性最后我把所有的都取一遍;
分享整理后的一個方法:
class StoredProcHelper { private static $type = [ 'integer'=>PDO::PARAM_INT, 'string'=>PDO::PARAM_STR, 'null'=>PDO::PARAM_NULL, 'boolean'=>PDO::PARAM_BOOL ]; private $sql = '';//此變量在下方說明 private $params = [];//此變量在下方說明 private $connect_info;//此變量在下方說明 private $pdo_connect; public function __construct($connect_info,$sql,$params){ $this->sql = 'SET NOCOUNT ON;'.$sql; $this->params = $params; $this->connect_info = $connect_info; if(!empty($this->connect_info->dsn) && !empty($this->connect_info->username) && !empty($this->connect_info->password)){ $this->pdo_connect = new PDO($this->connect_info->dsn,$this->connect_info->username, $this->connect_info->password); } } public function ExecuteProc(){ $link = $this->pdo_connect->prepare($this->sql); foreach ($this->params as $key => $value){ $link->bindParam($key,$value,self::$type[strtolower(gettype($value))]); } $link->execute(); $i = 1; $res[0] = $link->fetchAll(); while($link->nextRowset()){ $res[$i] = $link->fetchAll(); $i++; } return $res; } }
使用舉例:
public static function Example($connect_info,$mobile){ $sql='declare @customParam int;exec you_proc @Mobile = :mobile,@OutParam=@customParam out;select @customParam as outName;'; $params = [ ':mobile'=>$mobile ]; $pdo = new StoredProcHelper($connect_info,$sql,$params); $res = $pdo->ExecuteProc(); var_dump($res); }
變量$sql和$params的形式如例子中表現(xiàn)的;
變量$connect_info的形式如下【因為本人是在Yii框架 下使用的,所以以此變量是直接根據(jù)Yii來獲取數(shù)據(jù)庫鏈接配置來進(jìn)行的,如果自己有所不同可以自行更改形式以及賦值形式,在框架中方便的是不同環(huán)境下直接獲取配置能分別獲取到是sqlsrv和dblib,不需要自行去更改】:
[ 'dsn' => 'sqlsrv:Server=xxxxxxxxxx;Database=xxxxx', 'username' => 'xxxxx', 'password' => 'xxxxxxxxxxxxxxxxxxxx', 'charset' => 'utf8', ] //或 [ 'dsn' => 'dblib:host=xxxxxxxxxx;dbname=xxxxx', 'username' => 'xxxxx', 'password' => 'xxxxxxxxxxxxxxxxxxxx', 'charset' => 'utf8', ],
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP基于pdo的數(shù)據(jù)庫操作類【可支持mysql、sqlserver及oracle】
- PHP6連接SQLServer2005的三部曲
- PHP連接SQLServer2005的方法
- Win2003+apache+PHP+SqlServer2008 配置生產(chǎn)環(huán)境
- php使用pdo連接sqlserver示例分享
- PHP連接SQLServer2005方法及代碼
- Linux下php連接SQLServer 2000數(shù)據(jù)庫的配置方法
- php5.3中連接sqlserver2000的兩種方法(com與ODBC)
- php插入中文到sqlserver 2008里出現(xiàn)亂碼的解決辦法分享
- PHP連接SQLServer2005的實現(xiàn)方法(附ntwdblib.dll下載)
- PHP連接SQLSERVER 注意事項(附dll文件下載)
- PHP連接SQLServer2005 的問題解決方法
- 萬能密碼的SQL注入漏洞其PHP環(huán)境搭建及防御手段
相關(guān)文章
php中實現(xiàn)精確設(shè)置session過期時間的方法
這篇文章主要介紹了php中實現(xiàn)精確設(shè)置session過期時間的方法,需要的朋友可以參考下2014-07-07mysql_num_rows VS COUNT 效率問題分析
mysql_num_rows 和 count( * ) 都能統(tǒng)計總數(shù),那個能好一點呢?2011-04-04PHP+JS實現(xiàn)大文件切片上傳功能實現(xiàn)實例源碼
這篇文章主要介紹了PHP+JS實現(xiàn)大文件切片上傳功能實現(xiàn)實例源碼,需要的朋友可以參考下2023-05-05