失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php基于Yii框架使用PDO调用sqlserver存储过程办法介绍

php基于Yii框架使用PDO调用sqlserver存储过程办法介绍

时间:2021-12-26 10:46:12

相关推荐

php基于Yii框架使用PDO调用sqlserver存储过程办法介绍

后端开发|php教程

sqlserver,php,调用

后端开发-php教程由于业务这边存储过程一直在sqlserver上面,所以要用php去调用它,然而我们本地的是windows,而线上又是linux,一开始使用Yii框架的一些机制去调用发现在本地一直都是好的然而到线上就不行了,找了很多方案,最后找到了pdo这种方案,而本地使用的驱动是sqlsrv线上是dblib所以需要注意下链接pdo时的驱动形式,在取结果集的时候注意windows和linux好像有所不同,在我加上set nocount on后win若果直接取结果就可以拿到最后的,然而放到linux就没了,气死人的说,索性最后我把所有的都取一遍;

h5展示网站源码,ubuntu16.4乱码,tomcat服务器默认主页,爬虫小公主,24岁学php还有前途吗,seo 权重prlzw

分享整理后的一个方法:

视频教学系统源码,vscode怎么给html,ubuntu有趣命令,Tomcat被损坏,sqlite 字符串分组,爬虫处理字母数字验证码,php 字符串 字节,肇庆市seo,个人wiki系统 网站,网页特效开头从右向左的代码,微擎 模板缓存lzw

asp源码 根据日期生成流水号,vscode粘贴的代码报错,ubuntu编写udp,tomcat 服务接口乱码,爬虫设计 书籍,php转义字符空格,福州seo搜索优化学习,动态网站 开源,emblog免费模板网lzw

class StoredProcHelper{ private static $type = [ integer=>PDO::PARAM_INT, string=>PDO::PARAM_STR, ull=>PDO::PARAM_NULL, oolean=>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基于Yii框架使用PDO调用sqlserver存储过程办法介绍》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。