首先安装扩展

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文件

 // 账号数据库
    '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

在末尾追加

 /**
     * @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;,才能勉强拿到返回,在文章最后我给出了我最开始获取的结果集的方案例子,但是真的拉稀,你们可以看看,不要吐槽。

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的结果集

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;
}