Activities

May 2010
M T W T F S S
« Apr   Jun »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

How do I enable MSSQL extension on php

I have a requirement to enable MSSQL extension on php and need to access remote MSSQL server from the server. I think this requirement is very rare in case and need a dedicated hosting for this. Technically accessing database from different network will affect the application performance badly during the peak hours time. I will add the tutorial for how to enable the remote access on MSSQL server on another session.

OS : Centos
PHP build : Custom compilation, php-5.2.9

1. Download ODBC driver for Linux and install. You can find it from here

[root@rc-157 installation]# wget : http://www.unixodbc.org/unixODBC-2.3.0.tar.gz
[root@rc-157 installation]# tar -zxvf unixODBC-2.3.0.tar.gz
[root@rc-157 installation]# cd unixODBC-2.3.0
[root@rc-157 installation]# sh configure && make && make install

2. Download the freedts library

[root@rc-157 installation]##wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
[root@rc-157 installation]# tar -zxvf freetds-stable.tgz
[root@rc-157 installation]# cd freetds-0.82/
[root@rc-157 installation]# <strong>./configure --prefix=/usr/local  --sysconfdir=/usr/local/ --with-tdsver=8.0  --with-unixodbc=/usr/local --enable-msdblib</strong>
[root@rc-157 installation]# make &amp;&amp; make install

Sometimes you get a error during the

'freetds' compilation time when you are using Fedora. Best option to skip this error is use "

./configure –with-tdsver=8.0 –with-unixODBC=/usr/local”  on the configuration time.

Next steps is to build the mssql extension against the php build that already created. Go to php source directory which is used for the previous compilation. Here I uses the source folder under “/home/installation/php-5.2.9”

[root@rc-129 mssql]# cd /home/installation/php-5.2.9
[root@rc-129 php-5.2.9]# cd ext/mssql/
[root@rc-129 mssql]# pwd
/home/installation/php-5.2.9/ext/mssql
[root@rc-129 mssql]#phpize
[root@rc-129 mssql]#./configure –with-mssql
[root@rc-129 mssql]#make
[root@rc-129 mssql]#make install

[root@rc-129 mssql]# make install
Installing shared extensions: /usr/lib/extensions/no-debug-non-zts-20060613/

[root@rc-129 mssql]# vi /etc/php.ini
added the line “ extension=mssql.so” and ensure the extension_dir = “/usr/lib/extensions/no-debug-non-zts-20060613”
[root@rc-129 mssql]# php -v ” there is no error displayed”
PHP 5.2.9 (cli) (built: Aug 13 2009 17:51:53)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

[root@rc-129 mssql]# php -r ‘phpinfo();’ | grep “mssql”
mssql
mssql.allow_persistent => On => On
mssql.batchsize => 0 => 0
mssql.charset => no value => no value
mssql.compatability_mode => Off => Off
mssql.connect_timeout => 5 => 5
mssql.datetimeconvert => On => On
mssql.max_links => Unlimited => Unlimited
mssql.max_persistent => Unlimited => Unlimited
mssql.max_procs => Unlimited => Unlimited
mssql.min_error_severity => 10 => 10
mssql.min_message_severity => 10 => 10
mssql.secure_connection => Off => Off
mssql.textlimit => Server default => Server default
mssql.textsize => Server default => Server default
mssql.timeout => 60 => 60
PWD => /home/installation/php-5.2.9/ext/mssql
_SERVER[“PWD”] => /home/installation/php-5.2.9/ext/mssql
_ENV[“PWD”] => /home/installation/php-5.2.9/ext/mssql

[root@rc-129 mssql]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

Pls not that MSSQL extension can be enabled without unixodbc driver but you can’t establish the connection untill you not bind it with freedts installation.

You may use the following sample php file to test the connectivity.

 error_reporting(E_ALL);
$a = new dbConnect();
class dbConnect {
var $connId;
var $host;
var $db;
var $user;
var $pass;
#function dbConnect
function dbConnect() {
$this-&gt;host             =       '192.168.0.1:8000';
$this-&gt;db               =       'userdb';
$this-&gt;user             =       'username';
$this-&gt;pass             =       'pass';

$this->connId = mssql_connect($this->host, $this->user, $this->pass);
if (!$this->connId) {
trigger_error(“Error connecting to Server $this->host”, E_USER_ERROR);
die();
}else{
echo ‘Connected to MSSQL server sucessfully’;
}
if (!mssql_select_db($this->db, $this->connId)) {
trigger_error(“Unable to connect to database $this->db”, E_USER_ERROR);
die();
}
}
function __destruct(){
if ($this->connId){
@mssql_close($this->connId);
#print(“Database Connection Closed”);
}
}
}

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>