Accessing Microsoft SQL Database from Linux Boxes

In order to use Linux & MSSQL with php , we need to install a TDS driver which is Tabular Data Stream Protocol.

yum install freetds /usr/bin/tsql -S [mssql server name or ip] -U [username]

locale is “en-US.UTF-8″

locale charset is “UTF-8″

Password: [type your password]


Now you need to edit /etc/freetds.conf and add the following to the end of file :


vi /etc/freetds.conf

[SQLServer] host = [mssql server name or ip] port = 1433tds version = 8.0 client charset = UTF-8

Now setup ODBC Data source

First create a template file containing your FreeTDS setup information.<br></br><br></br>tdsdriver.template

[FreeTDS] Description = v0.62 with protocol v8.0 Driver = /usr/lib/

unixODBC needs to know about all ODBC drivers you intend to use. While you can use the GUI

program that comes with unixODBC, you can also use the odbcinst command. First

create a template file containing your FreeTDS setup information.<br></br>

odbcinst -i -d -f tdsdriver.template odbcinst: Driver installed. Usage count increased to 1. Target directory is /etc

This will add an entry to the end of the file /etc/odbcinst.ini

[FreeTDS] Description = v0.62 with protocol v8.0 Driver = /usr/lib/ UsageCount = 1

Now create a ODBC data source name, or DSN, so client applications can utilize the driver.<br></br><br></br>create a template then use odbcinst to create the DSN.


[MSSQLServer] Driver = FreeTDS Description = Sample Database Trace = No Server = [mssql.servername.or.ip] Port = 1433 Database = [database]

Run odbcinst, telling it to install the datasource entry using the template we just created.

odbcinst -i -s -f tdsdatasource.template

Test your ODBC connection using isql.

isql -v MSSQLServer [username] [password]

You should see something like this :

| Connected! | | | | sql-statement | | help [tablename] | | quit | | | +———————————-+ SQL>

Set it up for PHP and UTF-8

vi /etc/odbc.ini [MSSQLdatabase] Description = Sample Database Driver = FreeTDS Trace = No Servername = SQLServer #refers to the SQLServer in /etc/freetds.conf Port = 1433 Database = [database]

You also need to have php-odbc installed

rpm -qa | grep php-odbc


With the php-odbc module installed, in /etc/php.ini there should also be an entry under [ODBC]:

extension =

Create php page to connect to database :

if you want to run it as a shell script put this at the top

!/usr/bin/php -q