通过CodeIgniter连接到SQL Server

时间:2013-11-24 10:57:16

标签: codeigniter sqlsrv

我正在尝试设置Windows开发环境:Windows 8.1 with IIS 8.5 running SQL Server 2008RC2 and PHP 5.3.24

CodeIgniter 2.1.4

我可以通过普通PHP脚本中的PDO很好地连接到数据库。但是当我尝试通过CodeIgniter连接时,我收到此错误:

"Unable to connect to your database server using the provided settings.
Filename: C\inetpub\wwwroot\ci\system\database\DB_Driver.php
Line number 124"

如果我在IIS界面中打开PHP Manager,我可以看到启用了两个必需的驱动程序:

php_sqlsrv_53_ts.dll
php_pdo_sqlsrv_53_ts.dll

这两个都显示在我的ini文件中。

在我的CodeIgniter数据库配置文件中,我将驱动程序设置为:sqlsrv(如果我使用mssql,我会得到一个空白屏幕)。

我的CI安装中有/system/database/drivers/sqlsrv个驱动程序。

唯一奇怪的是,phpinfo()在配置命令下显示--without-mssql--without-pdo-mssql,但我知道它在CI之外工作。

phpinfo()还会在C:\WINDOWS显示ini文件路径,但它也会将加载的ini路径显示为C:\Program Files (x86)\PHP\v5.3\php.ini

我试过移动但没有运气。

我的/ application / config / database配置文件:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'localsql';
$db['default']['password'] = 'password';
$db['default']['database'] = 'elements';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$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;

我是否遗漏了具体的内容?

3 个答案:

答案 0 :(得分:4)

首先,改变

$db['default']['database'] = ‘elements;
$db['default']['dbdriver'] = ‘sq’lsrv;

$db['default']['database'] = 'elements';
$db['default']['dbdriver'] = 'sqlsrv';

在配置文件中。

其次,司机sqlsrv是错误的。打开/system/database/drivers/sqlsrv/sqlsrv_driver.php

要在配置中允许pconnect,请从

更改行89
$this->db_connect(TRUE);

return $this->db_connect(TRUE);

如果您想正确使用affected_rows,请从

更改行274
return @sqlrv_rows_affected($this->conn_id);

return @sqlsrv_num_rows($this->result_id);

我看到了如何修复affected_rows在其他位置发布的多个建议,但如果您还使用sqlsrv进行会话验证,则将_execute更改为不使用Scrollable会破坏存储的会话。< / p>

答案 1 :(得分:2)

如果有帮助,可以使用CodeIgnitor 2(我使用的是2.2.1版)连接到SQL Server的3种不同方式,这些方法适用于使用Windows 8.1,SQL Server 2012和PHP 5.3运行的IIS:

在我的情况下,我有一个名为&#34; MSSQLSERVER2012&#34;的命名SQL Server实例。如果您不使用命名实例,则可以在下面的所有3个示例中将(本地)\ MSSQLSERVER2012更改为(本地)。

方法#1:sqlsrv: 在database.php中使用这些设置:

$db['local_windows_sqlsrv']['username'] = 'username';
$db['local_windows_sqlsrv']['password'] = 'SQL_PASSWORD_HERE';
$db['local_windows_sqlsrv']['database'] = 'my_db_name';
$db['local_windows_sqlsrv']['hostname'] = '(local)\MSSQLSERVER2012';
$db['local_windows_sqlsrv']['dbdriver'] = 'sqlsrv';
$db['local_windows_sqlsrv']['dbprefix'] = '';
$db['local_windows_sqlsrv']['pconnect'] = FALSE;
$db['local_windows_sqlsrv']['db_debug'] = TRUE;
$db['local_windows_sqlsrv']['cache_on'] = FALSE;
$db['local_windows_sqlsrv']['cachedir'] = '';
$db['local_windows_sqlsrv']['char_set'] = 'utf8';
$db['local_windows_sqlsrv']['dbcollat'] = 'utf8_general_ci';
$db['local_windows_sqlsrv']['swap_pre'] = '';
$db['local_windows_sqlsrv']['autoinit'] = TRUE;
$db['local_windows_sqlsrv']['stricton'] = FALSE;

方法#2:与SQL Server的odbc: 首先进行此处显示的更改:https://stackoverflow.com/a/9617808/908677

然后在database.php中使用这些设置:

$db['local_windows_odbc']['database'] = 'my_db_name';
$db['local_windows_odbc']['hostname'] = 'Driver={SQL Server};Server=(local)\MSSQLSERVER2012;Database=' . $db['local_windows_odbc']['database'];
$db['local_windows_odbc']['username'] = 'username';
$db['local_windows_odbc']['password'] = 'SQL_PASSWORD_HERE';
$db['local_windows_odbc']['dbdriver'] = 'odbc';
$db['local_windows_odbc']['dbprefix'] = '';
$db['local_windows_odbc']['pconnect'] = FALSE;
$db['local_windows_odbc']['db_debug'] = TRUE;
$db['local_windows_odbc']['cache_on'] = FALSE;
$db['local_windows_odbc']['cachedir'] = '';
$db['local_windows_odbc']['char_set'] = 'utf8';
$db['local_windows_odbc']['dbcollat'] = 'utf8_general_ci';
$db['local_windows_odbc']['swap_pre'] = '';
$db['local_windows_odbc']['autoinit'] = TRUE;
$db['local_windows_odbc']['stricton'] = FALSE;

方法#3:使用SQL Server的PDO。

更新:此方法适用于建立连接和插入行,但要获得查询工作,您需要升级到CodeIgnitor 3.0,因为它会为PDO添加真正的SQL Server支持。

首先在system / database / drivers / pdo / pdo_driver.php中的__construct()中进行以下更改

替换:

empty($this->database) OR $this->hostname .= ';dbname='.$this->database;

使用:

if (strpos($this->hostname, 'sqlsrv') === FALSE)
{
   empty($this->database) OR $this->hostname .= ';dbname='.$this->database;
}

然后在database.php中使用这些设置:

// PDO with SQL Server
// IMPORTANT NOTE: requires mod to __construct() in pdo_driver.php to prevent appending ';dbname='.$this->database to hostname
$db['local_windows_pdo']['username'] = 'username';
$db['local_windows_pdo']['password'] = 'SQL_PASSWORD_HERE';
$db['local_windows_pdo']['database'] = 'my_db_name';
$db['local_windows_pdo']['hostname'] = 'sqlsrv:server=(local)\MSSQLSERVER2012;Database=' . $db['local_windows_pdo']['database'];
$db['local_windows_pdo']['dbdriver'] = 'pdo';
$db['local_windows_pdo']['dbprefix'] = '';
$db['local_windows_pdo']['pconnect'] = FALSE;
$db['local_windows_pdo']['db_debug'] = TRUE;
$db['local_windows_pdo']['cache_on'] = FALSE;
$db['local_windows_pdo']['cachedir'] = '';
$db['local_windows_pdo']['char_set'] = 'utf8';
$db['local_windows_pdo']['dbcollat'] = 'utf8_general_ci';
$db['local_windows_pdo']['swap_pre'] = '';
$db['local_windows_pdo']['autoinit'] = TRUE;
$db['local_windows_pdo']['stricton'] = FALSE;

答案 2 :(得分:1)

您的数据库配置中必须包含以下内容

$db['default']['driver']='sqlsrv'; //your mistake
$db['default']['database']='your_database'; // your mistake
$db['default']['pconnect']=false;