服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|R语言|

服务器之家 - 编程语言 - PHP教程 - thinkphp调用sqlserver储存过程返回多个结果集

thinkphp调用sqlserver储存过程返回多个结果集

2021-09-26 14:00weixin_42848765 PHP教程

这篇文章主要介绍了thinkphp调用sqlserver储存过程返回多个结果集,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

首先安装扩展

windows

分为两个步骤

  1. 找到对应自己PHP版本的pdo扩展,下载解压出来,并且在php.ini里面启用扩展,需要注意的问题是php版本以及是否为安全版本
  2. 下载 ODBC Driver https://docs.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017,这个没啥注意的,你是啥系统就下载啥安装包就行

linux 和 windows差不多,安装扩展的话直接可以用pecl

当你成功加载了可以在phpinfo()里面看到,当然了,如果你安装扩展这些都有诸多问题都话,~你可真拉稀。

thinkphp操作sqlsrv储存过程

我使用的tp版本是5.0和操作多个数据库,希望能对你有所帮助

配置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
// 账号数据库
 'UserDBConn' => [
   'type'      => 'sqlsrv',
   // 服务器地址
   'hostname'    => '139.129.1.1',
   // 数据库名
   'database'    => 'DB3',
   // 用户名
   'username'    => 'xxxx',
   // 密码
   'password'    => 'tt123!@#',
   // 端口
   'hostport'    => '5188'
 ],
 // 金币数据库
 'ScoreDBConn' => [
   'type'      => 'sqlsrv',
   // 服务器地址
   'hostname'    => '139.129.1.1',
   // 数据库名
   'database'    => 'DB2',
   // 用户名
   'username'    => 'xxxx',
   // 密码
   'password'    => 'tt123!@#',
   // 端口
   'hostport'    => '5188'
 ],
 // 记录数据库
 'RecordDBConn' => [
   'type'      => 'sqlsrv',
   // 服务器地址
   'hostname'    => '139.129.1.1',
   // 数据库名
   '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

查询和增删改都可以调用query,如果你没有想要获取的结果集的话可以调用execute()。

query()有一个弊端,如果你的绑定参数的形式(非参数绑定)是直接写进sql的话,他有可能会判断你这个不是一个储存过程;
具体实现请查看thinkphp/library/think/db/Connection.php:368行,当然也不会有结果集返回。

你也可以用调用procedure(),这个方法调用的话就一定会返回结果集。

起初我就是这个问题,并没有采用绑定参数的形式提交,直接写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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_42848765/article/details/103859844

延伸 · 阅读

精彩推荐