Reputation:
I want to build an application in Codeigniter and I want to use SQL Server 2012 as my database
I have not tried this yet and I don't know from where should I start.
How can I configure my Codeigniter to use SQL Server 2012 database?
Can I use Codeigniter Active records to retrive data from SQL Server database?
If I can not use Codeigniter, how can I use SQL Server query inside my Codeigniter models?
Please give me a detailed explanation with example code.
Thanks.
Upvotes: 5
Views: 2556
Reputation: 2885
In your application/config/database.php file:
$db['sqlserver']['username'] = 'db_username';
$db['sqlserver']['password'] = 'db_password';
$db['sqlserver']['database'] = 'db_database';
$db['sqlserver']['hostname'] = 'sqlsrv:server=db_servername;Database=' . $db['sqlserver']['database'];
$db['sqlserver']['dbdriver'] = 'pdo';
$db['sqlserver']['dbprefix'] = '';
$db['sqlserver']['pconnect'] = FALSE;
$db['sqlserver']['db_debug'] = TRUE;
$db['sqlserver']['cache_on'] = FALSE;
$db['sqlserver']['cachedir'] = '';
$db['sqlserver']['char_set'] = 'utf8';
$db['sqlserver']['dbcollat'] = 'utf8_general_ci';
$db['sqlserver']['swap_pre'] = '';
$db['sqlserver']['autoinit'] = TRUE;
$db['sqlserver']['stricton'] = FALSE;
The first four values obviously need to be set to whichever configuration you have (db_username
, db_password
, db_database
, db_servername
).
And set the active group:
$active_group = 'sqlserver';
Using PDO is generally more secure (https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059).
Don't forget to install/enable the sqlsrv PDO driver if it hasn't already been done. https://www.microsoft.com/en-us/download/details.aspx?id=20098 - If on Windows
Upvotes: 1
Reputation: 99
first do this https://docs.moodle.org/28/en/Installing_MSSQL_for_PHP then the database.php config file
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'user';
$db['default']['password'] = 'pass';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mssql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$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;
and it's done!
make sure that's server login is authenticated user and password not with windows login information
Upvotes: 0
Reputation: 845
@rajeev has one of some solutions, but if it didn't work you can try use ODBC connection. ODBC has function as gateway connection between your CodeIgniter application and SQL Server 2012 database.
change the value of 'sqlsrv' to 'odbc'.
$db['db']['dbdriver'] = 'odbc';
To create ODBC connection you can read this link : http://msdn.microsoft.com/en-us/library/ca6axakh%28v=vs.80%29.aspx
Upvotes: 0
Reputation: 4142
It can be done from setting of databases in config->database.php add own configuration of database like
//sql cofig setting
$db['rajeev']['hostname'] = 'localhost';
$db['rajeev']['username'] = 'rajeev';
$db['rajeev']['password'] = 'rajeev@123';
$db['rajeev']['database'] = 'rajeev_ci';
$db['rajeev']['dbdriver'] = 'sqlsrv';
$db['rajeev']['dbprefix'] = '';
$db['rajeev']['pconnect'] = TRUE;
$db['rajeev']['db_debug'] = TRUE;
$db['rajeev']['cache_on'] = FALSE;
$db['rajeev']['cachedir'] = '';
$db['rajeev']['char_set'] = 'utf8';
$db['rajeev']['dbcollat'] = 'utf8_general_ci';
$db['rajeev']['swap_pre'] = '';
$db['rajeev']['autoinit'] = TRUE;
$db['rajeev']['stricton'] = FALSE;
then set $active_group like this
$active_group = 'rajeev';
Upvotes: -1