简体   繁体   English

Codeigniter MSSQL / SQLSRV连接

[英]Codeigniter MSSQL/SQLSRV Connection

I'm working on a web app that connects to a MSSQL database. 我正在开发一个连接到MSSQL数据库的Web应用程序。 I previously managed to connect to the database without any problems, however I made a minor change to the sqlsrv_driver file. 我以前设法成功连接到数据库,但是对sqlsrv_driver文件进行了较小的更改。 I encountered the below error, rolled my changes back to the original file, but there are still issues. 我遇到以下错误,将更改回滚到原始文件,但是仍然存在问题。

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. 此外,我通过sqlsrv_connect函数在codeigniter外部建立了连接。 Which a successful connection is made with no errors. 哪个连接成功,没有错误。

Why am I getting this error? 为什么会出现此错误?

For connection from PHP->SQLSrv you need install these drivers : 为了从PHP-> SQLSrv连接,您需要安装以下驱动程序:

a. 一种。 Microsoft Drivers 3.0 for PHP for SQL Server (SQLSRV30.EXE) 适用于SQL Server的PHP的Microsoft驱动程序3.0 (SQLSRV30.EXE)

b. Microsoft Visual C++ 2010 Redistributable Package (32bits/64bits) Microsoft Visual C ++ 2010可再发行组件包(32位/ 64位)

c. C。 Microsoft® SQL Server® 2012 Native Client (sqlncli.msi) Microsoft®SQLServer®2012本机客户端 (sqlncli.msi)

And add this row in php.ini file: 并将此行添加到php.ini文件中:

extension=php_sqlsrv_54_ts.dll

The Driver sqlsrv is buggy. 驱动程序sqlsrv有问题。 Open /system/database/drivers/sqlsrv/sqlsrv_driver.php 打开/system/database/drivers/sqlsrv/sqlsrv_driver.php

To allow pconnect in your configuration, change line 89 from 要在您的配置中允许pconnect ,请将第89行更改为

$this->db_connect(TRUE);

to

return $this->db_connect(TRUE);

If you want to use affected_rows correctly, then change line 274 from 如果要正确使用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_rows posted elsewhere, but changing _execute to not use Scrollable will break stored sessions if you're also using sqlsrv for session validation. 我看到的是如何解决多个建议affected_rows其他地方张贴,但改变_execute不使用Scrollable ,如果您还使用SQLSRV会话验证将打破存储会话。

I just got this problem solved. 我刚刚解决了这个问题。 And I was connecting to MSSQL hosted with microsoft Azure 我正在连接到Microsoft Azure托管的MSSQL

Steps I followed after several research work on internet are as follows : 在进行了几项有关互联网的研究后,我遵循的步骤如下:

database.cfg : database.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. 主要是dbdriver,pcconnect和您应正确配置的主机名。 Rest are common. 休息很普遍。 Get hostname from your azure database details. 从您的azure数据库详细信息中获取主机名。

And I also modified couple of system files as I heard there were issues with DB driver. 而且我还修改了几个系统文件,因为听说数据库驱动程序存在问题。

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 :) 希望它能帮助有需要的人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM