thinkphp5 连接 SqlServer 运行存储过程,返回为空解决方案

匿名 (未验证) 提交于 2019-12-02 22:11:45
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a114469/article/details/84792192

项目做在win下,服务器是类似空间服务器,所以不可以装扩展,只能yum

水准不高 ,只能用最简单的方法 。

贴代码记录一下,返回结果成功 ,代码copy修改,其中有些不懂的地方 或者还有更好的方法,随时欢迎赐教

修改配置文件 database.php

 'db2'   => [         //本地         'type'            => 'sqlsrv',         // 服务器地址         'hostname'        => '127.0.0.1',         // 数据库名         'database'        => '****',         // 用户名         'username'        => 'sa',         // 密码         'password'        => 'root',         // 端口         'hostport'        => '1433',     ],

\thinkphp\library\think\db\connector\Sqlsrv.php

 $dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];

改为:

 $dsn = 'dblib:version=8.0;charset=utf8;dbname=' . $config['database'] . ';host=' . $config['hostname'];

\thinkphp\library\think\db\Query.php

添加方法:

 public function execute_sp($sql, $bind = [])     {         return $this->connection->execute_sp($sql, $bind);     }

\thinkphp\library\think\db\Connection.php

添加方法:

 /**      * 执行sqlserver sp语句      */     public function execute_sp($sql, $bind = [])     {         $this->initConnect(true);         if (!$this->linkID) {             return false;         }          // 记录SQL语句         $this->queryStr = $sql;         if ($bind) {             $this->bind = $bind;         }          //释放前次的查询结果         if (!empty($this->PDOStatement) && $this->PDOStatement->queryString != $sql) {             $this->free();         }          Db::$executeTimes++;         try {              $this->PDOStatement = $this->linkID->prepare($sql);             $this->bindParam($bind);              // 执行语句             $this->PDOStatement->execute();             $this->PDOStatement->nextRowset();              // 返回结果集             return $this->getResult(false, true);         } catch (\PDOException $e) {             if ($this->isBreak($e)) {                 return $this->close()->execute($sql, $bind);             }             throw new PDOException($e, $this->config, $this->getLastsql());         } catch (\Exception $e) {             if ($this->isBreak($e)) {                 return $this->close()->execute($sql, $bind);             }             throw $e;         }     }

自己找个地方随便测试一下

 public function test()     {         $sql = "sp语句";         $res = db2()->execute_sp($sql);         print_r($res);     }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!