ReynierPM
ReynierPM

Reputation: 18660

Unable to connect to local SQL Server Express instance from CodeIgniter 2.1.3, what I am missing?

I will start saying that I tried almost everything I could found over the Internet included the following list:

What I haven't tried? Use the Microsoft SQL Server extension as many answer says.

Here is how my database.php config looks like:

$active_group = 'mssql_local';

$db['mssql_local']['hostname'] = 'localhost:1433'; // RNDPC\SQLEXPRESS:1433
$db['mssql_local']['username'] = 'ssUser';
$db['mssql_local']['password'] = '******';
$db['mssql_local']['database'] = 'ssDB';
$db['mssql_local']['dbdriver'] = 'mssql';
$db['mssql_local']['dbprefix'] = '';
$db['mssql_local']['pconnect'] = false;
$db['mssql_local']['db_debug'] = false;
$db['mssql_local']['cache_on'] = false;
$db['mssql_local']['cachedir'] = '';
$db['mssql_local']['char_set'] = 'utf8';
$db['mssql_local']['dbcollat'] = 'utf8_general_ci';
$db['mssql_local']['swap_pre'] = '';
$db['mssql_local']['autoinit'] = true;
$db['mssql_local']['stricton'] = false;
$db['mssql_local']['port']     = 1433;

But I keep seeing the following "warning" on my logs:

ERROR - 2019-03-20 10:18:31 --> Severity: Warning --> mssql_connect(): Unable to connect to server: localhost:1433,1433 /var/www/html/sseditor/codeigniter/2.1.3/system/database/drivers/mssql/mssql_driver.php 63

Here is a snippet of the mssql_driver.php:

56  function db_connect()
57  {
58      if ($this->port != '')
59      {
60          $this->hostname .= ','.$this->port;
61      }
62
63      return @mssql_connect($this->hostname, $this->username, $this->password);
64  }

Does any have a better idea here? I am running out of them :|

Upvotes: 1

Views: 356

Answers (1)

ReynierPM
ReynierPM

Reputation: 18660

After hours of headache I find out the issue/solution. I was about to delete the question but I prefer to answer myself even if that is becomes a problem for my reputation but I am pretty sure can save someone time and effort.

First thing and I completely forgot to add this part to the OP (apologies for that) apache and PHP are running inside a CentOS VM (Vagrant+VBox).

The problem: the guest where the LAMP environment is running didn't know a thing about the host and makes sense.

The solution: add the host IP address and the NS to the /etc/hosts file on the guest.

190.168.56.1    RNDPC\SQLEXPRESS SQLEXPRESS

That fixed the issue regarding the following message:

Unable to connect to server: RNDPC\SQLEXPRESS:1433,1433

A second issue arise related to:

RNDPC\SQLEXPRESS:1433,1433

Because the driver if the parameter port is specified add that ,port.

The solution: remove the port parameter from the database.php and "hard code" the port at the end to the host definition.

Now my database.php looks like:

$db['mssql_common']['hostname'] = 'RNDPC\SQLEXPRESS:1433';
$db['mssql_common']['username'] = 'ssUser';
$db['mssql_common']['password'] = '*****';
$db['mssql_common']['database'] = 'ssDB';
$db['mssql_common']['dbdriver'] = 'mssql';
$db['mssql_common']['dbprefix'] = '';
$db['mssql_common']['pconnect'] = false;
$db['mssql_common']['db_debug'] = false;
$db['mssql_common']['cache_on'] = false;
$db['mssql_common']['cachedir'] = '';
$db['mssql_common']['char_set'] = 'utf8';
$db['mssql_common']['dbcollat'] = 'utf8_general_ci';
$db['mssql_common']['swap_pre'] = '';
$db['mssql_common']['autoinit'] = true;
$db['mssql_common']['stricton'] = false;

Hope this help someone in the future :)

Upvotes: 1

Related Questions