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

2025-05-27 0 78

首先安装扩展

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;

}

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 thinkphp调用sqlserver储存过程返回多个结果集 https://www.kuaiidc.com/70707.html

相关文章

发表评论
暂无评论