问题
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()
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.
I have just inherited this code base and am not very familiar with CI, but would appreciate any help on moving forward.
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);
but not using
$this->load->database('production');
print '<b>Database Version:</b> ' . $this->db->version() . '<br />';
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" } }
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;
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
回答1:
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';
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;
to:
if (strstr($this->hostname, 'sqlsrv') == TRUE){
$this->hostname;
} else {
$this->hostname .= ";dbname=".$this->database;
}
来源:https://stackoverflow.com/questions/25395300/no-db-connection-with-mamp-and-mssql-sql-server-using-codeigniter