php Codeigniter MSSQL/SQLSRV 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20398029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:09:47  来源:igfitidea点击:

Codeigniter MSSQL/SQLSRV Connection

phpsql-servercodeignitersqlsrv

提问by Ben

I'm working on a web app that connects to a MSSQL database. I previously managed to connect to the database without any problems, however I made a minor change to the sqlsrv_driver file. I encountered the below error, rolled my changes back to the original file, but there are still issues.

我正在开发一个连接到 MSSQL 数据库的 Web 应用程序。我以前设法连接到数据库没有任何问题,但是我对 sqlsrv_driver 文件做了一个小改动。我遇到了以下错误,将更改回滚到原始文件,但仍然存在问题。

I have ensured that I have relevant MS drivers installed, correct port number used, etc however its returning this error:

我已经确保我安装了相关的 MS 驱动程序,使用了正确的端口号等,但是它返回了这个错误:

Unable to connect to your database server using the provided settings.
Filename: D:...\system\database\DB_driver.php
Line Number: 124  

Further to this I have made a connection out side of codeigniter via the sqlsrv_connect function. Which a successful connection is made with no errors.

此外,我通过 sqlsrv_connect 函数在 codeigniter 外部建立了连接。成功连接没有错误。

Why am I getting this error?

为什么我收到这个错误?

回答by csotelo

For connection from PHP->SQLSrv you need install these drivers :

要从 PHP->SQLSrv 连接,您需要安装这些驱动程序:

a. Microsoft Drivers 3.0 for PHP for SQL Server(SQLSRV30.EXE)

一种。 Microsoft Drivers 3.0 for PHP for SQL Server(SQLSRV30.EXE)

b. Microsoft Visual C++ 2010 Redistributable Package (32bits/64bits)

Microsoft Visual C++ 2010 Redistributable Package (32bits/64bits)

c. Microsoft? SQL Server? 2012 Native Client(sqlncli.msi)

C。 微软?SQL服务器?2012 本地客户端(sqlncli.msi)

And add this row in php.ini file:

并在 php.ini 文件中添加这一行:

extension=php_sqlsrv_54_ts.dll

回答by Charity Leschinski

The Driver sqlsrv is buggy. Open /system/database/drivers/sqlsrv/sqlsrv_driver.php

驱动程序 sqlsrv 有问题。打开/system/database/drivers/sqlsrv/sqlsrv_driver.php

To allow pconnectin your configuration, change line 89from

pconnect在您的配置中允许,请89

$this->db_connect(TRUE);

to

return $this->db_connect(TRUE);

If you want to use affected_rowscorrectly, then change line 274from

如果你想affected_rows正确使用,那么换行274

return @sqlrv_rows_affected($this->conn_id);

to

return @sqlsrv_num_rows($this->result_id);

I saw multiple suggestions of how to fix affected_rowsposted elsewhere, but changing _executeto not use Scrollablewill break stored sessions if you're also using sqlsrv for session validation.

我看到了很多关于如何修复affected_rows发布在别处的建议,但是如果您还使用 sqlsrv 进行会话验证,更改_execute为不使用Scrollable会破坏存储的会话。

回答by PC.

I just got this problem solved. And I was connecting to MSSQL hosted with microsoft Azure

我刚刚解决了这个问题。我正在连接到微软 Azure 托管的 MSSQL

Steps I followed after several research work on internet are as follows :

在互联网上进行了几次研究工作后,我遵循的步骤如下:

database.cfg :

数据库.cfg:

$db['default']['hostname'] = 'XXXXXXX.database.windows.net';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'databasename';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Mainly dbdriver,pcconnect and the hostname you should configure properly. Rest are common. Get hostname from your azure database details.

主要是 dbdriver、pcconnect 和您应该正确配置的主机名。休息很常见。从 azure 数据库详细信息中获取主机名。

And I also modified couple of system files as I heard there were issues with DB driver.

我还修改了几个系统文件,因为我听说 DB 驱动程序有问题。

system/database/drivers/sqlsrv/sqlsrv_driver.php

系统/数据库/驱动程序/sqlsrv/sqlsrv_driver.php

function db_pconnect()
    {
        //$this->db_connect(TRUE);
        return $this->db_connect(TRUE);
    }

and

function affected_rows()
{
    //return @sqlrv_rows_affected($this->conn_id);
    return @sqlsrv_num_rows($this->result_id);
}

I was able to connect to database and create database application.

我能够连接到数据库并创建数据库应用程序。

Hope it will help someone in need :)

希望能帮到有需要的人 :)