PHP MS SQL ODBC on Debian

12:01 PM by Christian Koch

Filed under php

Last modified Friday November 6th, 2009

Short introduction on how to establish a MS SQL Server connection with PHP on Debian.

Install FreeTDS on Debian for PHP ODBC. Login as root.

First check if PHP’s ODBC extension is installed. If not install it with:

apt-get install php5-odbc

Install FreeTDS ODBC:

apt-get install tdsodbc

Don’t forget to restart your Apache after installing additional PHP extensions.

/etc/init.d/apache restart

Now we can test a connection to MS SQL Server.

<?php
$con = odbc_connect('Driver=FreeTDS;Server=IPTOMSSQLSERVER;Port=1433;Database=MyDatabase', 'databaseUser', 'databasePassword') or die("ups cannot connect LALA");

// do your stuff

odbc_close($con);
?>

That’s it HF

 

Related Articles

                                                          3 Comments

                                                          1. flo

                                                            Hey, theres some more config to do!
                                                            if u speak german, check this:
                                                            https://www.adminlife.net/allgemein/mssql-zugriff-unter-debian-etch-mit-unixodbc-und-freetds/comment-page-1/#comment-2418

                                                            else:
                                                            1. check /etc/odbc.ini
                                                            data source names (DSN) must be added here:

                                                            [MeinMSSQLServer]
                                                            Driver = FreeTDS
                                                            Description = MSSQL Test
                                                            Trace = Yes
                                                            TraceFile = /tmp/sql.log
                                                            ForceTrace = yes
                                                            Server = 11.11.11.11
                                                            Port = 1433
                                                            Database = DatabaseName

                                                            2. check /etc/odbcinst.ini
                                                            Path to library must fit:

                                                            [FreeTDS]
                                                            Description = MSSQL DB
                                                            Driver = /usr/lib/odbc/libtdsodbc.so
                                                            UsageCount = 1

                                                            Thursday November 26th, 2009 - 05:15 PM – Reply

                                                            • 1. Therefor the long DSN: Driver=FreeTDS;Server=IPTOMSSQLSERVER;Port=1433;Database=MyDatabase

                                                              Your way you just need to connect like this:

                                                              $con = odbc_connect('MeinMSSQLServer', 'databaseUser', 'databasePassword') or die("ups cannot connect LALA");
                                                              

                                                              2. /etc/odbcinst.ini should hopefully be installed along with apt-get install.

                                                              THX 4 your reply

                                                              Thursday November 26th, 2009 - 05:34 PM – Reply

                                                          2. Greg

                                                            Thank’s for this post (and comments!). it was very useful.

                                                            Monday January 18th, 2010 - 01:34 PM – Reply

                                                          Leave a Comment