PHP with MSSQL on Mac OS X Snow Leopard

I’ve recently needed to connect to an MS SQL database remotely from my Mac OS X development environment. I like to use the default PHP installation provided to me by Apple, as such I unsurprisingly found that the standard installation is missing the functions needed such as mssql_connect(). You will have this problem by default in OS X and it the response when you try to use and function prefixed by mssql will be:

Fatal error: Call to undefined function mssql_connect()

I began my quest to install the necessary packages a few hours ago and thought I would document the process here as I didn’t find anything specific to my needs.

My environment is a pretty vanilla Snow Leopard (10.6.5) PHP environment, using a standard PHP runtume and the Apache server provided by Apple, so no MAMP or Entropy PHP here. I found a great guide here designed for use with MAMP that I learned from to get it working for me.

First off I started by obtaining the latest stable source release of FreeTDS, you can get this by clicking here.

Once downloaded, expand the archive and visit the folder.

cd freetds-0.82
./configure --prefix=/private/etc/freetds --with-tdsver=8.0 --sysconfdir=/private/etc/freetds/conf
make
make install

Next you need to acquire the latest source release of PHP. Now I’ve only tested this with one release so you may need one that matches you installed version. For me I was running PHP 5.3.3 and it worked fine when I downloaded the 5.3.4 source code. Anyway you can obtain the source from here, download and expand as before.

Now this is where I stopped following the instructions from Scott and began playing around. In the terminal:

1
2
3
4
5
6
7
8
9
cd php-5.3.4
./configure --with-php-config=/usr/bin/php-config
make
cd ext/mssql/
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config --with-mssql=/private/etc/freetds
make
cd modules/
sudo cp mssql.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

What is different with the above is that I am forcing the use of the pre existing PHP installation to configure the compilation of the new MS SQL extension. Before I did this I was getting an error in the apache error log:


PHP Warning: PHP Startup: mssql: Unable to initialize module
Module compiled with module API=20060613
PHP compiled with module API=20090626
These options need to match
in Unknown on line 0

After completing the above compilation of the module, open your PHP.ini file, by default on OS X it will be located at /etc/php.ini. Open this file in your preferred editor and add the line ‘extension=mssql.so’.

After doing this restart your apache server by running ‘sudo apachectl restart’ and then create a phpinfo() page where you should see a new section that looks something like:

I hope this helps someone, if nobody else reads this I can claim it was for my own reference :) .

This entry was posted in PHP Tips, Uncategorized. Bookmark the permalink.

6 Responses to PHP with MSSQL on Mac OS X Snow Leopard

  1. G. Mark Stewart says:

    Has ANYONE gotten mssql_connect and mssql_query — MSSQL from PHP –
    to actually work on OS X? What happened with this effort? Did
    you actually succeed in reading a Microsoft SQL database? Am I
    the only bonehead not getting anywhere with this?

  2. Great post! Thank you for sharing this information. Just what I needed!

  3. Diego says:

    Great post man! You saved me a TON of time! I’ve been looking here and there but found no info as detailed and precise as yours. Thanks!

  4. Rob Ford says:

    This has saved my bacon twice now.
    I did have a little trouble tracking down freeTDS as the main site has gone awol. I eventually found it on Softpedia.
    Thanks so much.

  5. Kyle Barnes says:

    It seems that FREETDS is no longer available (or at least the site is down). I was able to download the CVS source from Source Forge, but I don’t believe it is exactly what I need.

    http://freetds.cvs.sourceforge.net/viewvc/freetds/

    After downloading the code I “CDed” to the directory and ran the following:

    ./configure –prefix=/private/etc/freetds –with-tdsver=8.0 –sysconfdir=/private/etc/freetds/conf

    No luck, however…perhaps I need to compile the code first?

    Any information would be greatly appreciated!

    Thanks!

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> <pre lang="" line="" escaped="">