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 .