How to connect Codeigniter with MSSQL (SQL Server)? - php

My server use Windows Server with MSSQL 2012. While I am use OS X (El Capitan) with XAMPP (Apache) for Mac and develop website using Codeigniter 2.2.0.
Here is my configuration :
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'xx.xx.xx.x';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = 'queue_sys';
$db['my_mssql']['dbdriver'] = 'mssql';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = TRUE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
but the results is :
Are my settings be wrong?
I just want to be able to connect to that server. Does anyone have any advice on solving this?

it will be helpful for someone
Detail steps for How to connect SQL Server with PHP in WampServer
step 1)
download the appropriate driver based on your php version, check using php_info() function, for me 5.6, so the appropriate driver is SQLSRV30, found in the following link
https://www.microsoft.com/en-us/download/details.aspx?id=20098
step 2) extract the drives in C:\wamp\bin\php\php5.6.19\ext
focus on the following .dll file they should be there, otherwise we can't connect with SQL, these are
php_sqlsrv_56_nts.dll and php_sqlsrv_56_ts.dll
step 3) enable the drives in php.ini
as follow, which is found in C:\wamp\bin\apache\apache2.4.18\bin as follow
extension=php_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_nts.dll
step 4) go to DB_driver.php line 96
found in
C:\wamp\www\public\system\database\DB_driver.php
please replace the mysqli by sqlsrv
public $dbdriver = ' mysqli ';
public $dbdriver = 'sqlsrv';
step 5) last and most important part, go to CI database config
in database.php file which's found in C:\wamp\www\public\application\config\database.php
adjust the params accordingly...
$db['default'] = array(
'dsn' => '',
'hostname' => 'ip address for sql server,port', // it should be SQL TCP enabled and firewall permitted for SQL port, whether default or custom.
'username' => 'your user name here',
'password' => 'your pwd here',
'database' => 'your db here',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'autoinit' => TRUE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
happy coding...

You may want to try odbc driver (built-in db driver in PHP). Mssql driver doesn't come convenient in PHP.
I got this working with CI 2.2.0 connected to MSSQL 2014 database (all Windows platform). I've also tried this using MSSQL 2012 before.
$active_group = 'my_mssql';
$active_record = TRUE;
$db['my_mssql']['hostname'] = 'Driver={SQL Server Native Client 11.0};Server=Host\Instance;Database=queue_sys;';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = '';
$db['my_mssql']['dbdriver'] = 'odbc';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = FALSE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;
Note:
SQL Server Native Client 11.0 or SQL Server Native Client 10.0, just play with both settings.
Server=xx.xx.xx.x usually is in the format Server=Host\Instance

The error is showing that SQLSRV driver implies the DLL is not loading ever.
check your php_info()
The SQLSRV driver requires for it.
check this for step by step solution
https://futbolsalas15.wordpress.com/2014/02/23/7-steps-to-make-sql-server-and-codeigniter-works/

Related

Codeigniter database help? PHP connect works

I'm using PHP 5.3.3 because I do not control the server. I'm also using CodeIgniter 3.1.0. I'm trying to use CI to connect to an Oracle server, Oracle 11g release 11.2.0.4.0.
I can connect just fine using php's oracle_connect:
$connect_string = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SID = MYSID)))";
$oconnect = oci_connect(USERNAME, PASSWORD, $connect_string );
$query = "select * from DB.TABLE";
$stid=oci_parse($oconnect, $query);
$r = oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
print_r($row);
}
But when I try to set up the db connection from within CodeIgniter, I get an error that the TNS name is not known, or that it can't connect, or that the service is unknown. Depends on how I tweak it.
Note that the examples on CodeIgniter may be wrong. The dsn uses SID= where the examples have SERVICE=; it wasn't until I read the user comments in the php manual that they suggested SID= for php's oci_connect.
Here's what I have for CodeIgniter:
$dsn_string = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SID = MYSID)))";
$db['default'] = array(
'dsn' => $dsn_string,
'hostname' => 'HOSTNAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DB',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Any ideas about what may be happening? I'm going to try a few other things as well. I could just ignore CI's db connection, but since I'm using it mainly as an api provider for an angular front end, the db is the biggest reason I'm even using CI.
Thanks!
#Tpojka - I have to use the server I'm given, but turns out this wasn't the issue.
The problem was the .htaccess file. I used an existing .htaccess and it had the following line:
DirectoryIndex index.php
I didn't think anything of it at first since that's a pretty normal line, but then I realized that I had changed my config.php to remove index.php from the url. As soon as I removed that line from .htaccess everything started working again.
Sheesh. I didn't think that could result in database errors, but that was the ultimate fix to get rid of the last of the errors.

Codeigniter error: Call to undefined function mysql_pconnect()

I have updated my codeigniter version from 2.2.4 step by step to 3.0.6 and I get an error:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function mysql_pconnect()
Filename: path-to-project\system\database\drivers\mysql\mysql_driver.php
Line Number: 135
Backtrace:
File: path-to-project\application\controllers\Main.php
Line: 10
Function: __construct
File: path-to-project\index.php
Line: 315
Function: require_once
I have just replaced my index.php file and system directory with the new one and made some changes in my application according to tutorial.
and this is the Main controller:
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
}
What causes the problem?!
And this is the link of the tutorial.
Deprecated features in PHP 5.5.x:
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.
You're using the deprecated mysql dbdriver. Locate the config/database.php file and change dbdriver to use mysqli :
$db['default']['dbdriver'] = 'mysqli';
Thanks to Anant
I come to a conclusion:
I completely changed my old database.php file in config folder with the new one:
From:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = '';
$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;
To:
$db['default'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
And the error is gone!
go to application/config/database.php
and just change mysql to mysqli
like
this was before:
$db['default']['dbdriver'] = 'mysql';
this was after solution:
$db['default']['dbdriver'] = 'mysqli';
i just change mysql to mysqli. that's it
my error was
Fatal error: Uncaught Error: Call to undefined function
mysql_pconnect() in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php:92
Stack trace: #0
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB_driver.php(116):
CI_DB_mysql_driver->db_pconnect() #1
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB.php(149):
CI_DB_driver->initialize() #2
E:\manish_data\software\xampp\htdocs\ci2\system\core\Loader.php(347):
DB(Array, NULL) #3
E:\manish_data\software\xampp\htdocs\ci2\application\models\usermodel.php(20):
CI_Loader->database() #4
E:\manish_data\software\xampp\htdocs\ci2\application\controllers\users.php(15): UserModel->getUsers() #5
E:\manish_data\software\xampp\htdocs\ci2\system\core\CodeIgniter.php(360):
Users->index() #6
E:\manish_data\software\xampp\htdocs\ci2\index.php(202):
require_once('E:\manish_data\...') #7 {main} thrown in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php
on line 92
If this error happened when you're hosting the website, make sure to set the correct PHP Version (the one your CI use).

Codeigniter 3 won't connect to MSSQL 2012 database under php 5.6 on Rackspace

Our site is on Rackspace cloud sites and is currently running fine with Codeigniter 3.0.0 on PHP 5.4.1.0 and connecting to a MSSQL 2012 database. Rackspace is in the process of upgrading PHP from 5.4.1.0 to 5.6.7-1 and has given testlinks for testing our websites in the new PHP environment. In this test environment, the site does not connect to the MSSQL database. However - no errors are thrown (that I can find, anyway!)
This is what I have for my database configuration:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mssqlXXXX.XXX',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE NAME',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
As a test, Rackspace also had me write a test controller. It also fails in Codeigniter. Code follows:
$username = "USERNAME";
$password = "PASSWORD";
$hostname = 'mssqlXXXX.XXX';
$dbname = "DATABASE";
//connection to the database
$dbcon = mssql_connect($hostname, $username, $password)or die("Unable to connect to MSSQL");
echo "Connected to MSSQL";
//select the database
mssql_select_db($dbname, $dbcon);
//SQL Select statement
$sqlselect = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
//Run the SQL query
$sqlquery = mssql_query($sqlselect);
//Output the query results
while ($result = mssql_fetch_array($sqlquery) )
{
echo "<br>";
print_r($result);
echo "<br>";
}
If I take this script out of the Codeigniter environment, it works.
I have tried changing my codeigniter database configuration as follows: hostname to an IP address instead of the Rackspace recommended value, changing the dbdriver to sqlsrv, using a dsn string instead of hostname and database and using the odbc database driver. Nothing works.
I am open to any and all suggestions.
EDIT
-------------------------------------------------------------------------
Rackspace says "issue with freetds, which is a library used my PHP's mssql module for connecting to MSSQL Databases; the issue appears to revolve around using passwords longer than 30 characters."
So - I shortened my password. Now the test controller mentioned above works! Hurrah! BUT - the standard Codeigniter database connection does not.
The issue was with Rackspace and they have corrected it.

Codeigniter Not Connecting to SQL Server

I'm trying to connect to an SQL server using CodeIgniter. If I use the sqlsrv driver - I get a fatal error message and if I use the odbc driver - I get an 'Unable to connect to your database server using the provided settings error message. Does anyone know how to fix this problem??? I don't mind how, I just want Codeigniter to connect to the SQL Server Database.
This is the database config file
$db['otherdb']['hostname'] = '195.234.10.55\SQLEXPRESS';
$db['otherdb']['username'] = 'username';
$db['otherdb']['password'] = 'password';
$db['otherdb']['database'] = 'ONEDB';
$db['otherdb']['dbdriver'] = 'odbc'; // Done this in both ODBC and SQLSRV
$db['otherdb']['dbprefix'] = '';
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = TRUE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = '';
$db['otherdb']['char_set'] = 'utf8';
$db['otherdb']['dbcollat'] = 'utf8_general_ci';
$db['otherdb']['swap_pre'] = '';
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Thanks
(Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search...)
A short step-by-step guide to connect to SQL_server from CodeIgniter 3 using native driver:
First of all download PHP for SQL_server Microsoft native driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098. Take care to choose the one that fits your setup.
Install it to the PHP extension directory.
Configure the new extension in PHP.ini. uncommenting or appending the line (depending on the choosen driver version). I.e.:
extension = php_sqlsrv_55_ts.dll
Restart Apache.
Now your PHP setup is able to connect to SQL_server databases.
Set up your CodeIgniter database connection by configuring application/config/database.php like
<?php
// native-driver connection
$db['default'] = array(
'dsn' => '',
'hostname' => 'SERVER\SQLEXPRESS',
'username' => 'test_user',
'password' => 'test_password',
'database' => 'test01',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => 'application/cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Tested from CodeIgniter 3.0.1 on SQL_server 2008 R2 and SQL_server 2014 express.

Codeigniter: Using PDO instead of mysql

Until Codeigniter implements the use of PDO, is there a way to use hack it into CI that's stable and secure? Currently, instead of using the db driver, I'm using a model instead which has all my PDO code like prepare, fetch, execute, etc. in it. What are the rest of you doing?
On CodeIgniter 2.1.4+ using MySQL databases (Edit the file: /application/config/databases.php).
To use PDO:
$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';
To use MySQLi
$db['default']['hostname'] = 'localhost';
$db['default']['dbdriver'] = 'mysqli';
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'mysql:host=127.0.0.1; dbname=****yourdatabasename*****; charset=utf8;',
'hostname' => '',
'username' => 'root',
'password' => '******yourpassword*******',
'database' => '',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Using PDO drivers instead of mysql require to change the hostname and the dbdriver like this :
$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';
CI, if used correctly, is both reliable and safe. Using PDO, while better if you are not using a framework, doesn't necessarily benefit you terribly over the CI_Database class.
If it really bothers you, you can swap out the mysql_*() functions for the equivalent mysqli_*() functions, but it really won't provide any discernable difference unless you are hyper-optimizing.
It should be noted that this can actually be done automatically by setting the dbtype appropriately (as Rocket notes below).
try php-activerecord I believe this use's PDO driver, its a simple plug and play via sparks.
Just a follow up to anyone having this same issue (including my future self), please ensure that whichever dbdriver you are loading has the correct library loaded in php.ini
$db['default']['dbdriver'] = 'mysqli'; //MySQLi <-- mysqli.dll
$db['default']['dbdriver'] = 'mysql'; //MySQL <-- mysql.dll
$db['default']['dbdriver'] = 'pdo'; //PDO <-- pdo.dll
Failure to load the correct dll will cause CodeIgniter to fail with a blank page.

Categories