Adding support for MS SQL Server to PHP in Linux

Adding support for MS SQL Server in PHP is not very difficult. Searching (Google/Bing/whatever) reveals lots of information on how to do this with Windows – naturally – but very little on how to go about it using Linux. Most people use precompiled PHP installations and I will show how to add MS SQL Server support to a precompiled PHP installation here. Those of you compiling PHP yourselves will probably understand what to do and what not based on the information here as well.

1. Install FreeTDS

First download and install FreeTDS from Use the following build commands to enable support for MS SQL Server (as root or using sudo).

./configure --enable-msdblib --prefix=/usr/local/freetds
make && make install

Unfortunately you need to tweak the installation somewhat as PHP still checks for files in FreeTDS that is no longer part of the installation. Just make sure these files exist (empty) by issuing the below commmands. (If you use another –prefix path above you will need to change the path accordingly)

touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a

2. Get the PHP source and compile the mssql extension

Yes – you need the complete PHP source even though you already have a precompiled PHP installed. You will not touch your PHP installation and we are not going to compile all of PHP. We need the source to be able to compile the mssql extension.

It is advised to always use the source of the same PHP version you have installed!

Unpack the source and compile the mssql extension. Remember again to change the path accordingly if you installed freeTDS in another location.

cd php*/ext/mssql
./configure --with-mssql=/usr/local/freetds

The extension should now be compiled and ready to install. You will find the binary in the immediate sub directory modules.

3. Install the extension

Find out where PHP expects to find extension libraries. The simplest way to check this is through the command line.

php -i | grep extension_dir

Some distributions use different php.ini files for command line PHP and the PHP web server module. So it might be good to double check using the function phpinfo() in a php script loaded through the web server (using your browser is easiest).

<?php phpinfo();

Then search for extension_dir in the configuration information displayed. For example, on my laptop running Ubuntu, the path is /usr/lib/php5/20060613+lfs.

Continuing from above without having moved away from the directory where you compiled your mssql extension.

cp modules/ /usr/lib/php5/20060613+lfs/

The extension is in the right place and all you have to do now is to make sure PHP actually loads it. To do this add the extension somewhere in the php.ini file. For example in the section Dynamic Extensions to keep it somewhere logical.

3. Restart the web server

If using the Apache 2 web server you would normally issue

/etc/init.d/apache2 restart

4. Post installation

Well that’s about it. You should have a workable mssql extension added to your PHP installation. You should be able to continue using your platforms chosen way of upgrading PHP without affecting the MS SQL Server support.

However, you might need to dig into the freetds.conf file. If you have followed my steps without altering the installation path you will find the freetds.conf file in /usr/local/freetds/etc/freetds.conf.

Sometimes it is difficult getting the connection work without adding it to the the freetds.conf. Especially since you may have to use different values for the tds version directive depending on the MS SQL Server version. Examples:

host = ntmachine.localdomain
port = 1433
tds version = 7.0

host =
port = 1433
tds version = 4.2

Again. Check that you are using the correct freetds.conf file and that you are using the correct tds version! More information on this at This and the above mentioned “missing files” that PHP is looking for are the two most common pitfalls.

Good luck!

Tagged with: , , ,
Posted in PHP
48 comments on “Adding support for MS SQL Server to PHP in Linux
  1. Marc Delisle says:

    I was using FreeTDS with PHP but had to stop due to its lack of support for encrypted connections to MS SQL Server. The firm responsible for this server suddently changed it to encrypted mode so I had to go back to PHP on a Windows server, plus their native MSSQL driver.

  2. pcdinh says:

    Does it support UTF-8 encoding?

  3. In olden times, the TDS install would use uppercase where PHP needed lowercase or vice versa.

    Not sure it that’s still true or not.

    Also, if the MSSQL driver is not working, install the Sybase driver.

    Microsoft bought the Sybase code of antiquity and added layer after layer after layer of the Next Bit Thing (Ole, Jet, etc) to it, just by wrapping yet another function call around the last failed not so big thing.

    The core code still runs the same as the Sybase. The one time they issued a patch that broke it, they got so much grief that they are unlikely to repeat *that* again.

    Anyway, long story short, Sybase driver works just fine, possibly slightly faster, as you bypass several layers of Microsoft cruft.

  4. Roky says:

    Nice article. I’ve come to similar solution when I was developing php application to connect to MSSQL database. My conclusions were posted on my blog. Altough text is in slovene maybe someone will be able to get some usufual information (use google translation).

  5. CH says:

    I think you need to change –enable=msdblib to –enable-msdblib

  6. Jay says:

    Great job, thanks very much!

    and yes -enable=msdblib should be –enable-msdblib

  7. Danne says:

    Yep – you’re right. I’ve fixed the typo in the post.

    It’s –enable-msdblib

  8. Alun Rowe says:

    Nice article but is there any chance you can expand up on how to get the latest version of PHP etc in step 2. I’m a Linux virgin and I’m trying to get this setup for my project testing.

  9. Alun Rowe says:

    Looking through it I’m stumped on number 1 too! help!

  10. SG says:

    Excellent instructions. I’d been racking my head about this for a few hours now.

    Thanks a million for posting.

  11. Razvan says:

    Great tutorial. Worked perfectly. Thank you

  12. Kuldip says:

    Thank you for the steps..

    i have setup this in my one ubntu pc, its working but in another pc i am getting error unable to connect Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server:

    While i am able to connect mssql from command line using tsql command.

  13. krlos says:

    Hey Kuldip, did you figure your problem out?

    Guess what? I am having the same problem as you…
    I can connect with sqsh, but when I try to use the mssql function it gives the same error:

    Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server:

    Am I missing something?

    Thanks in advance.

  14. Erax kariz says:

    Iam able to connect with
    tsql -H servername -p 1433 -U sa

    but when i try

    i am getting the same error
    Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server.
    anyone who solved this to help? I will really appreciate.

  15. John from Buffalo says:

    I am going down the path of MSSQL + PHP to avoid replication of data push through the MySQL 3.5.1 driver (which seems to be pretty badass under Windows 2003 server for us). This way I can pull directly from MSSQL vs. relying on my stored procedures that add a crazy level of complexity to the equation.

    Compiled my php configuration rather simply by adding the –with-mssql (which is a switch under the ./configure –help | grep “mssql” lookup). So, rather happy with this.

    Stop the server, restarted and it seems to be at least loaded. Time to go back and see if it actually worked, but the exploration in your article seems to be lacking down some examples to those who compile from source. Maybe giving an example for those who compile that can add the switch on the config.nice options after you’ve already done a compile once before .. might be nice.

    Otherwise good article thus far.

  16. KlaasV says:

    I’ve been using this method for a while, but when I set up a new server, with more recent Debian, Apache and PHP 5.2.6 versions, I had problems with WHERE-clauses in mssql_query SELECT statements.
    After three months searching the problem still isn’t solved, but I read that in PHP 5.3 support for mssql extension is dropped completely.
    Is there an alternative method?

  17. Mark Foster says:

    This was very helpful. Thanks.

  18. [...] Adding support for MS SQL Server to PHP in Linux [...]

  19. kitchen says:

    great article. what versions of sql server have been successfully tested with freetds?

  20. Charles Patterson says:

    I’ve had nothing but problems using mssql, however using odbc setup Linux/PHP talks to MSSQL without issue. Here’s how I set mine up:

  21. Kamran says:

    I’m trying to use this guide to install the mssql extension but I’m stuck. I get the following errors when I run the ‘make’ command in the mssql directory:

    /root/php-5.3.8/ext/mssql/php_mssql.h:68: error: redefinition of typedef ‘LPBYTE’
    /usr/local/freetds/include/sqlfront.h:35: error: previous declaration of ‘LPBYTE’ was here
    /root/php-5.3.8/ext/mssql/php_mssql.c: In function ‘php_mssql_do_connect’:
    /root/php-5.3.8/ext/mssql/php_mssql.c:767: warning: cast from pointer to integer of different size
    /root/php-5.3.8/ext/mssql/php_mssql.c: In function ‘php_mssql_get_column_content_without_type’:
    /root/php-5.3.8/ext/mssql/php_mssql.c:1120: warning: passing argument 1 of ‘spprintf’ from incompatible pointer type
    make: *** [php_mssql.lo] Error 1

  22. Paul says:

    I was able to compile the mssql module with freetds 0.91 by doing the following:
    Open php_mssql.h:
    Comment out the line that says “typedef unsigned char *LPBYTE;”
    Then try running “make” and see how it works!

  23. Matthew Oliveira says:

    Thanks very much for the information. Can anyone tell me what TCP/UDP ports need to be opened for a PHP script to use this method (through freetds) to query a SQL Server database on the same network as the server running the script?


  24. Dan says:

    I’ve been trying to get PHP to talk to mssql for a while now. The secret was in step one; configuring the FreeTDS with the mssql. Thanks a ton.

  25. deepak says:

    Hi all i have a doubt. I am not able to configure in the /php*/ext/mssql directory. The files inside that directory are ‘config.m4′ ‘config.w32′ ‘CREDITS ‘ ‘mssql.dsp ‘ ‘mssql_win32_howto.txt’ ‘php_mssql.c’ ‘ php_mssql.h’ . Am i missing something here, you said the “Unpack the source”. Do i need to copy the source php-5.4.3 directory again in to the mssql directory. Please help me out as i am in some kind of emergency to finish up this installation of php

  26. If some one desires to be updated with most recent technologies after that he must be pay a quick visit this web page and be up to date all the time.

    My website :: Sit4less Certified Coupon Code **

  27. As the admin of this web page is working, no question
    very soon it will be well-known, due to its quality contents.

    Take a look at my site Cult Furniture discount Code

  28. Everything is very open with a very clear description
    of the issues. It was definitely informative. Your website is very useful.
    Thank you for sharing!

    My weblog: AS Roma Store Coupon Code – -

  29. Thhe combination would slmetimes knock the imbibers out for a
    couple of days. Often described aas “a large dog in the body of a small dog,
    ” it is similar to the Norwegian Buhund and related to modern Welshi Corgis as well as Shetland Sheepdogs.
    Find Indian astrology with horoscope and live astrological prediction ,
    aries horoscope, daily, monthly, yearly analysis off birth
    chart and individual prediction of all planet with gemstone.

  30. hypnosis says:

    Hey all, I ran across your internet-site by using The search engines even though hunting for a identical make any difference, your internet-site came upward, it looks to be such as superior. I’ve saved in order to my favourites features and functions|added onto book marking.

  31. Adriano says:

    Precious information. Thank you!

  32. Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thank you

  33. posteeze.Com says:

    I know this web site gives quality based articles or reviews and additional material,
    is there any other web page which provides such information in quality?

  34. The employer benefits from improvement in employee morale and productivity and will also enjoy
    reduced medical and worker compensation costs. The competitive grant was provided by the National Center for Special Education Research (NCSER) of the Institute
    of Education Sciences (IES), the U. When you see about treatment for hear loss unfortunately, the type of hearing loss caused by over exposure to
    very loud noise is irreversible.

    my web page; tinnitus wiki (

  35. I’ve been exploring for a little bit for any high-quality
    articles or weblog posts on this kind of house . Exploring in Yahoo I eventually
    stumbled upon this web site. Reading this info So i’m glad to exhibit that I have a very
    just right uncanny feeling I came upon just what I needed.
    I such a lot indubitably will make certain to don?t omit this web site and provides
    it a glance on a relentless basis.

  36. If you wish for to take much from this paragraph then you have to apply
    such strategies to your won blog.

  37. The company is not designed too throw unnecessary ads or hype the market, its aim
    is to provide codes and just codes. I like stars and cosmic deigns quite a lot
    and I always find paper like thzt at those stores.
    Style Bakery is a site loadded with savings and tons
    of style information.

  38. Perhaps you have utilized by obtaining the Personal
    Wine Coupon Code just to acknowledge that you can not use this less expensive it gives?
    Anyone whom companies online uncomplicated Personal Wine Coupon Code
    would’ve professional this problem. These kinds of Personal Wine Coupon Code
    will generally be surprisingly cherished simply
    by internet purchasers thinking of that they make them wind
    up being worries in addition to expertise referring to relevant incomes.

    You can avoid nearly any sort of problems along with making use of a Personal Wine Coupon Code need to you
    know they will to make necessary techniques in contrast to they will.
    A great deal of the worries totally eaten with the help of customers

    Acquiring the done Personal Wine Coupon Code: The majority of Personal Wine Coupon Code utilize an occasion time-frame across
    that they could possibly stop made use of to obtain a more economical cost or simply just
    valuable approach. With each other, an extra specific somewhat back could possibly get
    fortuitous using your Personal Wine Coupon Code which contain passed her expiry day when your business have
    in fact never ever before deactivated this sort of reduce.

    Not essentially having a view at the Personal Wine Coupon Code absolutely.
    Prior to deciding to precisely just what precisely kind of Personal Wine Coupon Code should be made usage of anyone effectively
    could well aren’t delighted to learned discover the most from
    utilizing. You could not think almost all Personal Wine Coupon Code provide a created reduced.

    Going down getting an individual Personal Wine Coupon Code.
    On the numerous other hand, you could make
    use of software program applications keeping to the real Personal Wine Coupon Code to make sure that you could utilize them despite the truth that crucial.

    You can have a huge quantity great deals of cash on a routine month-to-month basis utilizing the right Personal
    Wine Coupon Code when you are acquiring online.

    Acquiring this kind of Personal Wine Coupon Code might not be uncomplicated.
    Have to be truth, you’re probably to be spending great
    deals of honest searching for people Personal Wine Coupon Code.

    You need to recognize using Personal Wine Coupon Code in the liable approach or
    potentially you do uncover youself to get spending a more considerable expenditure for anyone opportunities.
    It does not create just thinking about that to acknowledge a rather a lot of aspects being made conveniently
    readily available with a little asking expense as a result of Personal Wine
    Coupon Code.

    Perhaps you have made use of by obtaining the Personal Wine
    Coupon Code simply to acknowledge that you can not use this less expensive it supplies?

    Given that they make them come to be worries and also
    efficiency associating with vital profits, these sort of
    Personal Wine Coupon Code will typically be remarkably cherished merely by internet purchasers.

    Getting the done Personal Wine Coupon Code:
    Many Personal Wine Coupon Code use an event time-frame across that they
    can quit taken advantage of to get a much more low-cost cost or merely just useful strategy.
    Prior to deciding to what specifically kind of Personal Wine
    Coupon Code need to be utilized any person very well could well aren’t thrilled to figured out locate
    the most from taking advantage of.

    Perhaps you have used by obtaining the Personal Wine Coupon Code merely to identify that you can not
    use this less costly it provides? These kinds of Personal Wine Coupon Code will typically be incredibly valued just by internet purchasers thinking about that they make them end up being concerns
    along with knowledge referring to relevant earnings.

    Prior to picking to precisely just what exactly kind of Personal Wine Coupon Code
    should be used anybody efficiently could well aren’t delighted to discovered locate the most from making use of.
    When you are obtaining online, you could have a big quantity terrific supplies of
    money on a regular regular monthly basis making use of the ideal
    Personal Wine Coupon Code.

    Possibly you have used by obtaining the Personal Wine Coupon Code merely to identify that you can not utilize this cheaper it gives?

    These types of Personal Wine Coupon Code will generally be unbelievably valued just by web purchasers
    assuming about that they make them end up being worries with
    each other with knowledge referring to appropriate profits.

    Prior to choosing to specifically what precisely kind of Personal Wine Coupon Code have to be made usage of anyone effectively might well aren’t delighted to learned discover the most from utilizing.
    You could have a large quantity terrific bargains of
    money on a routine month-to-month basis utilizing the best Personal Wine
    Coupon Code when you are obtaining online. Getting this kind of Personal
    Wine Coupon Code might not be straightforward.

  39. I am regular visitor, how are you everybody? This post posted at this website
    is really pleasant.

  40. I like the valuable information you provide in your articles.
    I’ll bookmark your blog and check again here regularly.
    I’m quite sure I’ll learn many new stuff right here! Good luck for the next!

  41. I always used to study post in news papers but now as I
    am a user off internet so from now I am using nett
    for posts, thanks to web.

  42. All of us have these occasions in life that truly merely embarrassed us and we start wishing that we’re able to return in time and do things completely distinct so that we could have got results that are better.
    It possibly that time when you accepted a dare from your friend, or possibly the moment
    you forgot your greatest friend’s birthday. How about the moment when the statement
    came you were found out by you and when you had been on a day with the individual
    of your fantasies didn’t have the money to spend?
    Look when you actually want it is not only embarrassing
    it’s dismal we all have been there and lacking cash and it’s extremely annoying.
    Properly at eastern loans we provide cash advances which are dangerous to use.
    Whether you maybe get that one-of-a-kind tattoo you always had dreamed of our paydayloans, do
    a bit of grocery shopping or simply need to spend some bills Toronto support is constantly there to help you.
    Let us explain how it works. You start by completing
    our convenient and secure on-line application. Today in this program you allow us understand
    how much you want ranging anywhere from 500 dollars and more.
    Then you’re going to provide some basic details about yourself.
    Next you inform us which bank do you want us to deposit our cash in, a bank is needed by us
    or we will not be of any aid. Eventually you may give some employment information that is basic to us.
    Now once you have completed the program we direct you straight with their site and
    will match you to a lender according to their demands.
    The lender will subsequently exhibit charges, your loan,
    terms and conditions for your acceptance. Today once you are
    authorized you will possess the cash immediately deposited into your bank
    balances in as fast as 24 hours. Today we know also that everybody does not have
    a spotless credit rating and that’s also fine. Asian Loans payday loans expected future revenue simply to make certain you have the capacity to pay
    back your mortgag

  43. Rich Watkins says:

    Hi there i am kavin, its my first occasion to
    commenting anywhere, when i read this article
    i thought i could also make comment due to this sensible post.

  44. says:

    These accessories assist in ankle positioning and stopping foot
    defects. The Adidas Supernova Glide 5 and Supernova Trouble 5 athletic
    shoe deliver several of the exact same benefits as the Electricity Boost footwears but at a slightly lower
    price. An enormous thanks goes out in order to all the
    fantastic performers who place these evacuate for social make use of.

  45. Now I am ready to do my breakfast, later than having my breakfast coming yet again to read more news.

    My blog post … at home business

  46. Winston says:

    First off all I want to say great blog! I had a quick question that I’d like
    to ask if you don’t mind. I was interested to know how you center yourself and clear your mind before writing.
    I have had difficulty clearing my mind in getting my ideas ouut there.
    I truly do enjoy writing however it just seems like the
    first 10 to 15 minutes are generally wasted just trying to
    figure out how to begin. Any ideas or hints? Many thanks!

    Here is my web site: online courses (Winston)

10 Pings/Trackbacks for "Adding support for MS SQL Server to PHP in Linux"
  1. [...] Linux zu kompilieren und einzubinden. Danne Lundqvist hat dazu in seinem Blog einen entsprechenden Artikel [...]

  2. says:

    Adding support for MS SQL Server to PHP in Linux…

    Adding support for MS SQL Server in PHP is not very difficult. Searching (Google/Bing/whatever) reveals lots of information on how to do this with Windows – naturally – but very little on how to go about it using Linux. …

  3. [...] 1/6/2010 Update Adding support for MS SQL Server to PHP in Linux [...]

  4. [...] Lundqvist has a new post showing how to get MS SQL Server support to work with your PHP installation on linux with the help [...]

  5. [...] Adding support for MS SQL Server to PHP in Linux [...]

  6. [...] here: Adding support for MS SQL Server to PHP in Linux January 5th, 2010 | Tags: but-very, linux, little-on-how, nformation-on-how, not-very, php, [...]

  7. [...] are a lot of tutorials out there on how to build the extension yourself on Mac or Linux machine (this site for example), but none seemed to work for [...]

  8. [...] Adding support for MS SQL Server to PHP in Linux This entry was posted in Linux, Programming, Uncategorized, Web Designing by mehrdust. Bookmark the permalink. [...]

  9. [...] Винрарная статья по установке [...]

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>