如何将codeigniter连接到SQL服务器?

时间:2013-10-18 22:30:20

标签: sql-server database codeigniter database-connection

我看过很多帖子,但实际上没有。有人可以提供干净/清晰的方式将Codeigniter与Mac上的MS SQL Server连接吗?这是一个很高的水平,但我还没有看到一个清楚地证明这一点的解决方案。非常感谢!

2 个答案:

答案 0 :(得分:0)

我已经在您的application/config/database.php中完成了此操作,请执行以下操作:

// This address is valid if you're on the same domain as the server:
// If not, you can put the ip address or url here as well.
$db['default']['hostname'] = 'DBComputerName\SQLEXPRESS';

// if you need a non default port uncomment and edit
//$db['default']['port'] = '5432';

$db['default']['username'] = 'databaseUsername';
$db['default']['password'] = 'databasePassword';
$db['default']['database'] = 'DatabaseName';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;

我知道这是多么令人沮丧,作为一个提醒,并非所有Active Record功能都按预期工作或工作,这是由于许多SQL服务器怪癖。

我建议将大多数Active Record查询切换为:$this->db->query();

PS 如果我没记错,MSSQL没有预配置用户名,您需要创建用户名并允许从子网外部进行数据库访问。

请参阅此以创建db用户:
http://technet.microsoft.com/en-us/library/aa337545.aspx
并允许访问:
http://support.webecs.com/KB/a868/how-do-i-configure-sql-server-express-to-allow-remote.aspx

答案 1 :(得分:0)

我使用PDO驱动程序......这对我有用:

    $db['mssql']['hostname'] = 'sqlsrv:server=myserverIP;database=mydb;encrypt=true;trustservercertificate=true';
    $db['mssql']['username'] = 'myun';
    $db['mssql']['password'] = 'mypw';
    $db['mssql']['database'] = 'mydb';
    $db['mssql']['dbdriver'] = 'pdo';
    $db['mssql']['dbprefix'] = '';
    $db['mssql']['pconnect'] = TRUE;
    $db['mssql']['db_debug'] = TRUE;
    $db['mssql']['cache_on'] = FALSE;
    $db['mssql']['cachedir'] = '';
    $db['mssql']['char_set'] = 'utf8';
    $db['mssql']['dbcollat'] = 'utf8_general_ci';
    $db['mssql']['swap_pre'] = '';
    $db['mssql']['autoinit'] = TRUE;
    $db['mssql']['stricton'] = FALSE;