php 如何将 Codeigniter 与 MSSQL (SQL Server) 连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37694308/
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
How to connect Codeigniter with MSSQL (SQL Server)?
提问by bennysantoso
My server use Windows Server with MSSQL 2012. While I am use OS X (El Capitan) with XAMPP (Apache) for Mac and develop website using Codeigniter 2.2.0.
我的服务器使用 Windows Server 和 MSSQL 2012。虽然我使用 OS X (El Capitan) 和 XAMPP (Apache) for Mac 并使用 Codeigniter 2.2.0 开发网站。
Here is my configuration :
这是我的配置:
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'xx.xx.xx.x';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = 'queue_sys';
$db['my_mssql']['dbdriver'] = 'mssql';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = TRUE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
but the results is :
但结果是:
Are my settings be wrong?
我的设置有问题吗?
I just want to be able to connect to that server. Does anyone have any advice on solving this?
我只是希望能够连接到该服务器。有没有人对解决这个问题有任何建议?
回答by duduwe
You may want to try odbc driver (built-in db driver in PHP). Mssql driver doesn't come convenient in PHP.
您可能想尝试 odbc 驱动程序(PHP 中的内置数据库驱动程序)。Mssql 驱动程序在 PHP 中并不方便。
I got this working with CI 2.2.0 connected to MSSQL 2014 database (all Windows platform). I've also tried this using MSSQL 2012 before.
我使用 CI 2.2.0 连接到 MSSQL 2014 数据库(所有 Windows 平台)。我之前也使用 MSSQL 2012 尝试过这个。
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'Driver={SQL Server Native Client 11.0};Server=Host\Instance;Database=queue_sys;';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = '';
$db['my_mssql']['dbdriver'] = 'odbc';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = FALSE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
Note:
笔记:
SQL Server Native Client 11.0
or SQL Server Native Client 10.0
, just play with both settings.
SQL Server Native Client 11.0
或者SQL Server Native Client 10.0
,只玩这两种设置。
Server=xx.xx.xx.x
usually is in the format Server=Host\Instance
回答by Nassa
it will be helpful for someone
这对某人有帮助
Detail steps for How to connect SQL Server with PHP in WampServer
WampServer中如何用PHP连接SQL Server的详细步骤
step 1)
第1步)
download the appropriate driver based on your php version, check using php_info() function, for me 5.6, so the appropriate driver is SQLSRV30, found in the following link https://www.microsoft.com/en-us/download/details.aspx?id=20098
根据您的 php 版本下载相应的驱动程序,使用 php_info() 函数进行检查,对于我来说是 5.6,因此相应的驱动程序是 SQLSRV30,可在以下链接中找到 https://www.microsoft.com/en-us/download/details .aspx?id=20098
step 2) extract the drives in C:\wamp\bin\php\php5.6.19\ext focus on the following .dll file they should be there, otherwise we can't connect with SQL, these are
step 2) 解压C:\wamp\bin\php\php5.6.19\ext中的驱动器重点关注下面的.dll文件,它们应该在那里,否则连接不上SQL,这些是
php_sqlsrv_56_nts.dll and php_sqlsrv_56_ts.dll
php_sqlsrv_56_nts.dll 和 php_sqlsrv_56_ts.dll
step 3) enable the drives in php.ini as follow, which is found in C:\wamp\bin\apache\apache2.4.18\bin as follow
步骤3)在php.ini中启用驱动如下,在C:\wamp\bin\apache\apache2.4.18\bin如下
extension=php_sqlsrv_56_ts.dll extension=php_sqlsrv_56_nts.dll
扩展名=php_sqlsrv_56_ts.dll 扩展名=php_sqlsrv_56_nts.dll
step 4) go to DB_driver.php line 96 found in C:\wamp\www\public\system\database\DB_driver.php please replace the mysqli by sqlsrv
步骤 4) 转到 C:\wamp\www\public\system\database\DB_driver.php 中的 DB_driver.php 第 96 行,请将 mysqli 替换为 sqlsrv
public $dbdriver = ' mysqli ';
public $dbdriver = 'sqlsrv';
step 5) last and most important part, go to CI database config in database.php file which's found in C:\wamp\www\public\application\config\database.php adjust the params accordingly...
步骤 5)最后也是最重要的部分,转到位于 C:\wamp\www\public\application\config\database.php 中的 database.php 文件中的 CI 数据库配置,相应地调整参数...
$db['default'] = array(
$db['default'] = 数组(
'dsn' => '',
'hostname' => 'ip address for sql server,port', // it should be SQL TCP enabled and firewall permitted for SQL port, whether default or custom.
'username' => 'your user name here',
'password' => 'your pwd here',
'database' => 'your db here',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'autoinit' => TRUE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
);
happy coding...
快乐编码...
回答by Tejas Mehta
The error is showing that SQLSRV driver implies the DLL is not loading ever. check your php_info() The SQLSRV driver requires for it. check this for step by step solution
该错误表明 SQLSRV 驱动程序暗示 DLL 从未加载过。检查您的 php_info() SQLSRV 驱动程序需要它。检查这一步一步的解决方案
https://futbolsalas15.wordpress.com/2014/02/23/7-steps-to-make-sql-server-and-codeigniter-works/
https://futbolsalas15.wordpress.com/2014/02/23/7-steps-to-make-sql-server-and-codeigniter-works/