5

I have installed Xampp with a CodeIgniter installation. I want to connect from CodeIgniter to a SQL database.

I changed the database config file and set the dbdriver to sqlsrv.

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'IP Adress;
$db['default']['username'] = 'DBUserName';
$db['default']['password'] = 'DBPassword';
$db['default']['database'] = 'DBName';
$db['default']['dbdriver'] = 'sqlsrv';
$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;

In my controller I have the following code to try the connection:

$this->load->database();

$db_obj = $this->db->load('sql_Test',TRUE);
$connected = $db_obj->initialize();

if (!$connected){
    $db_obj = $this->d->load('yyy',TRUE);
} 
else{
    die('connected');
}

I have the following error:

Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\system\database\drivers\sqlsrv\sqlsrv_driver.php on line 76

I have read on a forum that I have to change line 89 from sqlsrv_driver.php:

function db_pconnect()
{
    // $this->db_connect(TRUE); original
    return $this->db_connect(TRUE);
}

What do I wrong?

Marten
  • 1,376
  • 5
  • 28
  • 49

2 Answers2

14

EDIT- First you need to download the driver http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

Now go to your XAMPP installation and search for php.dll It will display correct PHP dll you have.

1) move following files to xampp/php/ext directory.

php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll

2) If you have php5ts.dll then move following files to xampp/php/ext directory.

php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll

above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.

1) If you have php.dll then move following files to xampp/php/ext directory.

php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll

2) If you have php5ts.dll then move following files to xampp/php/ext directory.

php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll

Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.

extension=php_sqlsrv_53_nts_vc9.dll
extension= php_pdo_sqlsrv_53_nts_vc9 .dll 

Save the ini files and restart XAMPP


$check= @$CI->load->database($config, TRUE); // ommit the error
if ($check->call_function('error') !== 0) {
    // Failed to connect
}

I don't know for sure what you are trying to do but in codeigniter you don't need to initialise database, CI automatically does it for you

so-

$this->load->database();

$db_obj = $this->db->load('SQL_Test',TRUE);
$connected = $db_obj->initialize();

this is not needed.

You just need to load the model and in model start performing queries. $this->load->database(); In controller you need to load the model like-

$this->load->model('my_model');

then call the model function in which you have written the queries.

$this->my_model->myfunction(); 
Marten
  • 1,376
  • 5
  • 28
  • 49
sunny
  • 1,156
  • 8
  • 15
  • Yes, but I want to try the connection before I write a query. – Marten Apr 03 '14 at 08:19
  • I have made a model and I have written a query, but I get the same error. – Marten Apr 03 '14 at 08:29
  • @MartenB check i had edited my answer, i hope it helps – sunny Apr 03 '14 at 08:30
  • @MartenB Ok now i understand, FOr connection to sqlsrv.. you need to define it first in XAMPP. wait i will edit my answer – sunny Apr 03 '14 at 08:36
  • Thank for your explanation. I get the following error when I restart Xamp: Unable to load dynamic library php_sqlsrv_53_nts_vc9.dll. This dll isn't also with the files I downloaded from the website. – Marten Apr 03 '14 at 08:57
  • perhaps you may be making mistake while loading the .dll files, please check again! – sunny Apr 03 '14 at 09:05
  • I get this error before I run my query, even before Xampp started up. I will check it again :) – Marten Apr 03 '14 at 09:09
  • @Marten Biesheuvel Now i finally figured out what mistake you are doing, remove everything you have added about vc9.dll above. it is not supported in your version, also, you havent got it downloaded, so only add vc6.dll, remove everyline which contains vc9.dll in the above steps, and again restart XAMPP – sunny Apr 03 '14 at 09:14
  • I changed everything, but I still get the error that file not exists. – Marten Apr 03 '14 at 09:50
  • It works finally. I had the wrong php version (5.4). Thanks a lot! – Marten Apr 03 '14 at 12:21
  • Pleasure! m glad you finally had your code running! -:) – sunny Apr 03 '14 at 12:23
  • @Marten Biesheuvel I recommend that you change the heading of your question to "Connect sqlsrv in Xampp" ! In that way it can be more helpful for other users in future! – sunny Apr 03 '14 at 12:33
  • i also got same error Unable to load dynamic library php_sqlsrv_53_nts_vc9.dll. Please help – Er Sahaj Arora Nov 17 '20 at 09:17
1

Your XAMPP setup doesn't have sqlsrv support. You need to enable it in php.ini.

extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll
Gerben Jacobs
  • 4,515
  • 3
  • 34
  • 56
  • I have the following error when I restart Xampp: Unable to load dynamic library php_sqlsrv_53_nts_vc9.dll. This dll isn't also with the files I downloaded from the website (http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx) . – Marten Apr 03 '14 at 08:59