FreeTDS Connection Error with PHP and UnixODBC Troubleshooting

I have fought all this weekend with this hassle that is configuring the FreeTDS to correct work with PHP and unixODBC. Yeah, in this case we are talking all Linux.

Most of the times you will see something like this (actually the error message varies a bit, but…)

[unixODBC][FreeTDS][SQL Server]Unable to connect to data source.

What happens is that your data source configuration is either wrong or in the wrong place (yours trully).

To help you troubleshoot the error that you are getting, check the following configurations:

  • /etc/odbc.ini
  • /etc/odbcinst.ini
  • /usr/local/freetds/freetds.conf

Update: Make sure that you are editing the correct freetds.conf file. For that you can use tsql -C that will show you all variables that are enabled in compile time, including the location of the freetds.conf.

These 3 configurations together will construct your data source and when PHP with unixODBC and PDO-DBLIB tries to connect it will run all of those configurations to construct the connection. If you have a wrong configuration you will have that error.

Another thing that you can troubleshoot is checking the FreeTDS configuration in compile time ( tsql -C ). Check if all configuration is as it should. My issue was related to this. One of the configurations that I was missing was to enable freetds to work with msdblib module.

Other thing is to check if you have installed the unixODBC-dev package (source). When you install FreeTDS with unixODBC it will look for a sql.h library. This library is part of the unixODBC-dev package. Install it and it should be good to go.

I’m using Xubuntu 8.10 and 99% of the things that I have installed to make this work where done by the Symatic Package installer.

Hope it helps you as much as it did help me and if you have any questions, post it here and I will try to help you out as much as I can.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

4 responses to “FreeTDS Connection Error with PHP and UnixODBC Troubleshooting

  • Joao Victor

    When I tsql -C that does not show at all freetds.conf location, it even mention freetds.conf. I’m using Cent OS 5 and my freetds.conf is located in /etc/freetds.conf. I’ve been googling around for a week and can’t find a solution for my problem. When I isql the DataSource it works fine but when I try using PHP I get the same error you get.

    thanks

    • mcloide

      Use updatedb (you must be root for this) to update the locate db. It might take some time, but after you can run locate freetds.conf again and it should appear for you.

  • David M

    Thanks for posting this. I’ve been banging my head all day on getting tsql to work. In my case it worked fine when I specified the host, port, etc. But wouldn’t work with the -S option. It wasn’t until I read your blog post and ran the -C that I saw that it was using the freetds.conf file in /usr/local/etc/ and not the one in /etc/. Moving my settings to this correct conf file sorted out my problems.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: