Asked  7 Months ago    Answers:  5   Viewed   32 times

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:xampphtdocssystemdatabasedriverssqlsrvsqlsrv_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?

 Answers

21

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(); 
Wednesday, March 31, 2021
 
erotsppa
answered 7 Months ago
95
$this->db->where('who', 5);
$this->db->where('whos', 1);
$this->db->or_where('whos', 5);
$this->db->where('who', 1);

$this->db->delete('friendconnect');

This is also an alternative:

$this->db->where('who', 5)->where('whos', 1)->or_where('whos', 5)->where('who', 1);
$this->db->delete('friendconnect');

This also:

$this->db->where(array('who'=>5, array('whos'=>1)))->or_where('whos', 5)->where('who', 1);
$this->db->delete('friendconnect');

More info: here

Wednesday, March 31, 2021
 
ManojGeek
answered 7 Months ago
30

I searched the net for a while.. After posting this question for 10m, I finally found a unofficial driver..

Hope this works, I will check this out..

You can find it on: http://robsphp.blogspot.be/2012/06/unofficial-microsoft-sql-server-driver.html

Thursday, July 29, 2021
 
EastSw
answered 3 Months ago
78

mssql_connect() is no longer supported by PHP since PHP 7.0 and was depreciated in 5.3.

Create a phpinfo.php file:

<?php

// Show all information, defaults to INFO_ALL
phpinfo();

?>

Open the page, and on the first page that's opened, third from the bottom, check if the registered streams contains 'sqlsrv':

Registered PHP Streams  php, file, glob, data, http, ftp, zip, compress.zlib, phar, sqlsrv

If it doesn't then you have to enable the module. Try uncomment this code that you changed: From:

; On windows: extension_dir = "D:xamppphpext" "

To:

On windows: extension_dir = "D:xamppphpext"

And let us know the outcome.

Saturday, July 31, 2021
 
Gopal Biswas
answered 3 Months ago
20

If you haven't set a password for MySQL (check by opening the MySQL config through XAMPP), just put "" as the password. Make sure your PHP script is also configured to use that as the password, because it is currently trying to use one (hence the Using password: YES).

Friday, September 3, 2021
 
Tushar Garg
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :