无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了PHP基于PDO调用sqlserver存储过程的方法。分享给大家供大家参考,具体如下:
由于业务这边存储过程一直在sqlserver上面,所以要用php去调用它,然而我们本地的是windows,而线上又是linux,一开始使用Yii框架的一些机制去调用发现在本地一直都是好的然而到线上就不行了,找了很多方案,最后找到了pdo这种方案,而本地使用的驱动是sqlsrv线上是dblib所以需要注意下链接pdo时的驱动形式,在取结果集的时候注意windows和linux好像有所不同,在我加上set nocount on后win若果直接取结果就可以拿到最后的,然而放到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的形式如例子中表现的;
变量$connect_info的形式如下【因为本人是在Yii框架 下使用的,所以以此变量是直接根据Yii来获取数据库链接配置来进行的,如果自己有所不同可以自行更改形式以及赋值形式,在框架中方便的是不同环境下直接获取配置能分别获取到是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', ],
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】