The last 2 days I've spent a lot of time trying to figure out how to connect to MS SQL server hosted on Azure using PHP from an Ubuntu 14.04 server.
The code responsible to initiate the connection to the remote database is the following:
public function connect()
{
try
{
$this->databaseConnection = new \PDO(
'dblib:host='.$this->hostname.':'.$this->port.';dbname='.$this->databaseName.';',
$this->username,
$this->password
);
$this->databaseConnection->setAttribute(
\PDO::ATTR_ERRMODE,
\PDO::ERRMODE_EXCEPTION
);
}
catch (\PDOException $e)
{
throw new \Exception('Failed to connect to the database: '.$e->getMessage());
}
}
And every time I try to connect I receive:
Failed to connect to the database: SQLSTATE[01002] Adaptive Server connection failed (severity 9)
The problem is that this is a very general error and there's no way for me to understand what's causing the issue.
This is my /etc/freetds.conf configuration:
[placeholder.database.windows.net]
host = placeholder.database.windows.net
port = 1433
tds version = 7.2
client charset = UTF-8
And this is the tsql -C
output:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
In addition to this, I tried making some test using mssql_*
functions and even if I was able to successfully connect to the database most of the queries didn't work.