使用CodeIgniter不能与MAMP和MSSQL(SQL Server)建立数据库连接 [英] No DB connection with MAMP and MSSQL (SQL Server), using CodeIgniter

查看:52
本文介绍了使用CodeIgniter不能与MAMP和MSSQL(SQL Server)建立数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在OS X上运行MAMP 3.0,并尝试连接到远程SQL Server DB时没有运气.我已经从其他支持问题中获取了关于在MAMP php安装中启用mssql的信息.一切似乎都正确,mssqlsupport显示为在phpinfo()中启用

I'm running MAMP 3.0 on OS X and trying to connect to a remote SQL Server DB with no luck. I've followed what I could from other support questions regarding getting mssql enabled in my MAMP php install. Everything appears to be correct, mssqlsupport shows as enabled in phpinfo()

该项目使用sqlsrv驱动程序在CodeIgniter中,并且在运行数据库连接测试时,我在屏幕上什么都没有看到,并且MAMP日志或CI日志中没有错误.所有调试和错误报告似乎都已打开.

The project is in CodeIgniter using the sqlsrv driver, and when running a db connection test I get nothing on screen and no errors in my MAMP logs or my CI logs. All debugging and error reporting appears to be turned on.

我刚刚继承了此代码库,并且对CI不太熟悉,但是希望对前进有所帮助.

I have just inherited this code base and am not very familiar with CI, but would appreciate any help on moving forward.

已更新:在没有CI DB类的情况下进行查询时,我能够获取数据

UPDATED: I was able to get back data when querying without the CI DB class like so

$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-> load

and here is a dump of $this->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().

When I investigated the sqlsrv_driver.php I can see that it is failing when trying to use @sqlsrv_connect but it is not returning any errors. It dies before continuing at all. I have tried exiting the script with errors after it attempts to create $res but nothing is returned. It is never getting beyond @sqlsrv_connect().

$res = @sqlsrv_connect($hostname, $connectionInfo);

    if(!$res){
        $this->_set_error(sqlsrv_errors());
    }
    return $res;

这是我的配置/数据库设置:

Here are my config/database settings:

$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)的失败是主要问题

Worth noting, everything works fine on the actual production site, but it is a Windows machine running Apache (and PHP). There was also a custom sqlsrv driver written for CodeIgniter in the installation, to muddy things even more. Still, I believe the failure of @sqlsrv_connect($hostname, $connectionInfo) is the main problem, so far

推荐答案

我已经成功使用PDO和类似的db config通过CodeIgniter连接到MSSQL:

I've had success connecting to MSSQL with CodeIgniter using PDO and something like this for the 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行:

Unfortunately, you'll also need to change core/drivers/pdo/pdo_driver.php line 82 from:

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

收件人:

if (strstr($this->hostname, 'sqlsrv') == TRUE){
    $this->hostname; 
} else {
    $this->hostname .= ";dbname=".$this->database;
}

这篇关于使用CodeIgniter不能与MAMP和MSSQL(SQL Server)建立数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆