Adding support for MS SQL Server to PHP in Redhat / Linux

 Problem Description : 

 Establish mssql_connect function in PHP 5.3 on Redhat EL 5. Microsoft now uses SqlSrv driver for Win32 platforms so to use the MSSQL extension on Unix/Linux you first need to build and install the FreeTDS library. This shows how to add MS SQL support to a precompiled PHP installation (RPM package install).

 

 

 Resolution : 

 

1. Install FreeTDS

 

First download, unpack and install FreeTDS (download from freetds.org). Use the following build commands to enable support for MS SQL Server (as root or using sudo). cd to FreeTDS folder and run these commands...

 

./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

 

You need the complete PHP source even though you already have a precompiled PHP (5.3.3) installed. You will not touch your existing PHP installation and will not 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! In our case 5.3.3.

 

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

 

cd php*/ext/mssql

phpize

./configure --with-mssql=/usr/local/freetds

make

 

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

 

I received the following errors when I ran the ‘make’ command the first time 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

 

Found a post detailing a fix for this error and commented out the line indicated below. Was then able to run 'make'...

 

Open php_mssql.h in mssql directory

Comment out the line that says /* typedef unsigned char *LPBYTE; */

Then run “make” again.

 

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

 

On our Redhat installation of PHP 5.3 the path is /usr/lib64/php/modules

 

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

 

cp modules/mssql.so /usr/lib64/php/modules (copy compiled module to PHP extension folder)

 

The extension is now in the right place so all you have to do now is to make sure PHP loads it. To do this add the extension in the php.ini file under the Dynamic Extensions section. The php.ini file is located under /etc.

 

extension=mssql.so

 

3. Restart the web server

 

apachectl restart

 

4. Post installation

 

You should now have a workable mssql extension added to your PHP installation. You now must add the SQL Server you wish to connect to within the freetds.conf file located at /usr/local/freetds/etc/. You may have to use different values for the tds version directive depending on the MS SQL Server version. 

 

Examples:

 

[logisticsServer]

host = ntmachine.localdomain

port = 1433

tds version = 7.0

 

[intranetServer]

host = 192.168.1.145

port = 1433

tds version = 4.2

 

Working Example:

 

[RAPTOR]

host = raptor.gdn.peachnet.edu

port = 1433

tds version = 7.2

 

 

 

Versions of the TDS Protocol, by Product

Revision Date : 4/9/2013