PHP – Connecting to MS SQL

How to connect PHP to a MS SQL server from a Linux server

 

Install PHP and FreeTDS etc..

get php up and running with apache – then

# apt-get install php5-sybase php-pear php5-dev

Download FreeTDS from http://www.freetds.org/

# tar zxvf freetds-stable.tgz
# cd freetds-0.6.4/
# ./configure --prefix=/usr/local/freetds
# make
$ make install

 

Create the /usr/local/freetds/freetds.conf file

put this in the file:

# A typical Microsoft SQL Server 2000 configuration
[MyMSSQLDB]
host = 192.168.0.123
port = 1433
tds version = 8.0

And here is the PHP code:

<?php
putenv('TDSVER=80');
putenv('FREETDSCONF=/usr/local/freetds/etc/freetds.conf');
$db_conn = mssql_connect("MyMSSQLDB","sa","<password>") 
or die( "<strong>ERROR: DB Connection failed</strong>" );
?>

Leave a Reply

Your email address will not be published. Required fields are marked *