我在OS X上运行MAMP 3.0并尝试连接到远程SQL Server数据库而没有运气。我已经从我在MAMP php安装中启用mssql的其他支持问题中得到了我的帮助。一切似乎都是正确的,mssqlsupport在phpinfo()
中显示为已启用该项目使用sqlsrv驱动程序在CodeIgniter中,并且在运行数据库连接测试时,屏幕上没有任何内容,我的MAMP日志或CI日志中没有错误。所有调试和错误报告似乎都已打开。
我刚刚继承了这个代码库,并且对CI不是很熟悉,但是对于前进的任何帮助表示感谢。
更新:我在没有CI DB类的情况下查询时能够获取数据
$dbhandle = mssql_connect('host', 'user', 'password')
or die("Couldn't connect to SQL Server.");
$selected = mssql_select_db('mydb', $dbhandle) or die("Couldn't open database.");
$query = "SELECT TOP 5 user_id, username FROM users";
$result = mssql_query($query);
但未使用
$this->load->database('production');
print '<b>Database Version:</b> ' . $this->db->version() . '<br />';
这是$ this-&gt; load
的转储object(CI_Loader)#13 (12) { ["_ci_ob_level"]=> int(0) ["_ci_view_path"]=> string(71) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/views/" ["_ci_library_paths"]=> array(3) { [0]=> string(77) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/third_party/" [1]=> string(65) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/" [2]=> string(60) "/Users/jojo/Websites/cms/inetpub/codeigniter/system/" } ["_ci_model_paths"]=> array(2) { [0]=> string(77) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/third_party/" [1]=> string(65) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/" } ["_ci_helper_paths"]=> array(3) { [0]=> string(77) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/third_party/" [1]=> string(65) "/Users/jojo/Websites/cms/inetpub/codeigniter/uploader/" [2]=> string(60) "/Users/jojo/Websites/inetpub/codeigniter/system/" } ["_base_classes"]=> array(12) { ["benchmark"]=> string(9) "Benchmark" ["hooks"]=> string(5) "Hooks" ["config"]=> string(6) "Config" ["log"]=> string(3) "Log" ["utf8"]=> string(4) "Utf8" ["uri"]=> string(3) "URI" ["router"]=> string(6) "Router" ["output"]=> string(6) "Output" ["security"]=> string(8) "Security" ["input"]=> string(5) "Input" ["lang"]=> string(4) "Lang" ["loader"]=> string(6) "Loader" } ["_ci_cached_vars"]=> array(0) { } ["_ci_classes"]=> array(0) { } ["_ci_loaded_files"]=> array(0) { } ["_ci_models"]=> array(1) { [0]=> string(11) "httprequest" } ["_ci_helpers"]=> array(0) { } ["_ci_varmap"]=> array(2) { ["unit_test"]=> string(4) "unit" ["user_agent"]=> string(5) "agent" } }
当我调查sqlsrv_driver.php时,我可以看到它在尝试使用@sqlsrv_connect时失败,但它没有返回任何错误。它在继续之前就已经死了。我尝试创建$ res后尝试退出脚本但是没有返回任何内容。永远不会超越@sqlsrv_connect()。
$res = @sqlsrv_connect($hostname, $connectionInfo);
if(!$res){
$this->_set_error(sqlsrv_errors());
}
return $res;
以下是我的配置/数据库设置:
$active_group = 'production';
$active_record = TRUE;
$db['production']['hostname'] = 'XXX';
$db['production']['username'] = 'XXX';
$db['production']['password'] = 'XXX';
$db['production']['database'] = 'XXX';
$db['production']['dbdriver'] = 'sqlsrv';
$db['production']['dbprefix'] = '';
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = TRUE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = '';
$db['production']['char_set'] = 'UTF-8';
$db['production']['swap_pre'] = '';
$db['production']['autoinit'] = TRUE;
$db['production']['stricton'] = FALSE;
值得注意的是,在实际的生产网站上一切正常,但它是运行Apache(和PHP)的Windows机器。在安装过程中还有一个为CodeIgniter编写的自定义sqlsrv驱动程序,甚至更多的东西。不过,我认为@sqlsrv_connect($ hostname,$ connectionInfo)的失败是主要问题,到目前为止
答案 0 :(得分:0)
我已经成功使用PDO与CodeIgniter连接到MSSQL,对于db config也是如此:
$db['production']['hostname'] = 'sqlsrv:server=yourserver;database=yourdb;encrypt=true;trustservercertificate=true';
$db['production']['dbdriver'] = 'pdo';
不幸的是,您还需要更改core / drivers / pdo / pdo_driver.php第82行:
empty($this->database) OR $this->hostname .= ';dbname='.$this->database;
为:
if (strstr($this->hostname, 'sqlsrv') == TRUE){
$this->hostname;
} else {
$this->hostname .= ";dbname=".$this->database;
}