Skip to content

Connect phpGrid to MS SQL Server from Linux

When connecting phpGrid to SQL Server from Linux, it's common to receive a warning message similar to the following:

Warning: odbc_connect() [function.odbc-connect]: SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect.

Solution:


First of all, be sure to install Microsoft Drivers for PHP for SQL Server. 

https://www.microsoft.com/en-us/download/details.aspx?id=20098

In addition, to access MSSQL from *nix, install FreeTDS and unixODBC, 

sudo apt-get unixodbc unixodbc-dev freetds-dev tdsodbc

And then configurate freetds.conf, odbc.ini and odbcinst.ini 

--- /etc/odbc.ini ---
[TS]
Description = "test"
Driver = FreeTDS
Server = SERVER_NAME
Port = 1433
Database = DBNAME
ReadOnly = No
TDS_Version = 4.2

[Default]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

--- /etc/odbcinst.ini ---
[FreeTDS]
Description = FreeTDS Driver v0.91
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
fileusage=1
dontdlclose=1
UsageCount=1
Trace = yes
TraceFile = /tmp/FreeTDS.log
ForceTrace = yes

--- /etc/freetds/freetds.conf ---
[TS]
host = SERVER_NAME
port = 1433
tds version = 7.0
client charset = UTF-8

Lastly, it's important to include the following lines in the conf.php for Sql server to work (change the file path to both .ini accordingly)

// conf.php putenv("ODBCINSTINI=/usr/local/Cellar/unixodbc/2.3.1/etc/odbcinst.ini"); putenv("ODBCINI=/usr/local/Cellar/unixodbc/2.3.1/etc/odbc.ini");

Feedback and Knowledge Base