PHP/MSSQL/FreeTDS

Filed Under (Articles) by WebScHoLaR on 19-02-2008

Tagged Under : , ,

Freetds is a set of libraries for Unix and Linux that allows your programs to natively talk to Microsoft SQL Server. A clinet requested to have it compiled with PHP so I did it for him and wrote a guide to be used in future. I am writing the installation of freetds and then php compilation with mssql support.

INSTALLING FREETDS

1-. Download freetds -> www.freetds.org
2-. tar -zxvf freetds-stable-tgz
3-. cd freetds-0.62.3
4-. ./configure –prefix=/usr/local/freetds –with-tdsver=8.0 –enable-msdblib –enable-dbmfix –with-gnu-ld

Note: tdsver=8.0 if you use SQL 2000, tdsver=7.0 if you use SQL 7.0

5-. make
6-. make install
7-. /usr/local/freetds/bin/tsql -S <ip of the server> -U <User SQL>

Note: For default User SQL is sa and the it have not password

For example: /usr/local/freetds/bin/tsql -S 198.168.100.2 -U sa

8-. Add the next text in freetds.conf ( /usr/local/freetds/etc )
[TDS]
host = <ip of the Server with Sql>
port = 1433
tds version = 8.0

Note: If you use SQL 2000 then tds version = 8.0, if you use SQL 7.0 then tds version = 7.0

9-. Add the next text in the file /etc/ld.so.conf
/usr/local/freetds/lib

INSTALLING APACHE

1-. Download latest apache www.apache.org
2-. tar -zxvf httpd-*.tar.gz
3-. cd httpd-*
4-. ./configure –enable-module=ssl –enable-shared=rewrite –prefix=/usr/local/apache –enable-module=so
5-. make
6-. make install
7-. Configure the file -> httpd.conf ( /usr/local/apache/conf/httpd.conf )
8-. Probe the apache: /usr/local/apache/bin/apachectl start
/usr/local/apache/bin/apachectl stop

INSTALLING PHP

1-. Download the latest PHP in this site (www.php.net)
2-. tar -zxvf php-*.tar.gz
3-. cd php-*
4-. ./configure –with-apxs=/usr/local/apache/bin/apxs –with-xml –enable-bcmath –enable-calendar –with-curl –enable-ftp –with-jpeg-dir=/usr/local –with-png-dir=/usr –with-xpm-dir=/usr/X11R6 –enable-mbstring –enable-mbstr-enc-trans –enable-mbregex –enable-magic-quotes –with-mysql –with-pear –enable-sockets –enable-track-vars –with-ttf –with-freetype-dir=/usr –enable-gd-native-ttf –with-zlib –enable-versioning –with-mssql=/usr/local/freetds –enable-libxml

If php configure fails with the error Directory /usr/local/freetds is not a FreeTDS installation directory, then the solution is:

cp /usr/local/src/freetds-xxx/include/tds.h /usr/local/freetds/include
cp /usr/local/src/freetds-xxx/src/tds/.libs/libtds.a /usr/local/freetds/lib

5-. make
6-. make install
7-. cp php.ini-DIST /usr/local/lib/php.ini
8-. Add the next line in /usr/local/apache/conf/httpd.conf

AddHandler application/x-httpd-php .php .php4 .php3
AddType application/x-httpd-php .php
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .php3
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .phtml

DirectoryIndex index.html index.wml index.cgi index.shtml index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml index.htm default.htm default.html home.htm

TESTING

<html>
<body>
<?php
$con = mssql_connect (“<ip of the server SQL>”, “sa”, “”);
mssql_select_db (“<Data Base>”, $con);
$sql= “SELECT * FROM <Table>”;
$rs= mssql_query ($sql, $con);
echo “The field number one is: “;
echo mssql_result ($rs, 0, 0);
mssql_close ($con);
?>
</body>
</html>

REFERENCES

Couple of related URLs are mentioned below:

  1. http://www.php.net/manual/en/ref.mssql.php
  2. http://www.freetds.org/userguide/config.htm
  3. http://my.php.net/manual/en/function.mssql-connect.php
  4. http://linuxjournal.com/article/6636

Comments:

One Response to “PHP/MSSQL/FreeTDS”


  1. [...] Another good tutorial to use is: http://webscholar.net/2008/02/19/php-mssql-freetds/ [...]

Leave a Reply

You must be logged in to post a comment.

Proudly Hosted by eServicesProvider