Connect to msssql server from linux server using php
By CreativeSolutions
Instructions:
Step 1: FreeTDS
Download FreeTDS
Unpack the tarball
$]cd freetds-VERSION (where version is the FreeTDS version number)
$]./configure --prefix=/usr/local/freetds --enable-msdblib
$]make
$]su root
$]make install
$]make clean
Step 2: SQL Server PHP extension
Download the source for the right version of PHP (http://php.net/downloads)
Unpack the tarball
$]cd php-VERSION/ext (where VERSION is the PHP version number)
Copy the mssql/ directory to someplace where you can compile it from
CD to that directory
$]phpize (this requires the php-devel package)
$]./configure --with-mssql=/usr/local/freetds
$]make
$]su root
$]make install
$]make clean
If php is already installed and mssql not added follow run the below command
$yum install php-mssql
Step 3: FreeTDS configuration
$]vi /etc/freetds.conf (any editor available can used for this)
Go to the end of the file
Add the following lines
;--- Custom MSSQL server name ---
; THIS CAN BE ANY NAMED SERVER REFERENCE YOU WANT TO CREATE
[SERVERNAME]
; This is the host name of the MSSQL server
host=HOSTNAME
; This is the port number to use
port=PORTNUMBER
; This is the TDS version to use for anything over Server 2000
tds version=8.0
Save the file and exit
$]vi /etc/ld.so.conf (Again, any editor should be able to do this)
Go to the end of the file
Add the following line:
/usr/local/freetds/lib
Save the file and exit
Step 4: Enabling MSSQL PHP extension
$]cd /etc/php.d
If there is no file named mssql.ini create one
Edit the file and enter the following lines if not already there
; Enable mssql extension module
extension=mssql.so
Save the file and exit
Step 5: Apache (This should be done any time PHP changes)
$]/etc/rc.d/init.d/httpd stop
$]/etc/rc.d/init.d/httpd start
NOTE: $]apachectl -k graceful might work as well, though httpd start offers a status flag
Step 6: Test a file at the CLI and at the Web Server
I used the below code and got the result "Version: Microsoft SQL Server 2005 - 9.00.5000.00 (Intel X86) Dec 10 2010 10:56:29 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)"
< ?php
$server = 'wic018q.server-sql.com:4656';
// Connect to MSSQL
$link = mssql_connect($server, 'vs328460_1_dbo', 'Uq9y6hz3QX');
if (!$link) {
die('Something went wrong while connecting to MSSQL'.mssql_get_last_message());
}
$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);
echo "
Version: ".$row[0];
// Clean up
mssql_free_result($version);
? >
Please visist the below url for more details
http://www.robert-gonzalez.com/2008/03/31/connecting-php-on-linux-to-mssql-on-windows/
Step 1: FreeTDS
Download FreeTDS
Unpack the tarball
$]cd freetds-VERSION (where version is the FreeTDS version number)
$]./configure --prefix=/usr/local/freetds --enable-msdblib
$]make
$]su root
$]make install
$]make clean
Step 2: SQL Server PHP extension
Download the source for the right version of PHP (http://php.net/downloads)
Unpack the tarball
$]cd php-VERSION/ext (where VERSION is the PHP version number)
Copy the mssql/ directory to someplace where you can compile it from
CD to that directory
$]phpize (this requires the php-devel package)
$]./configure --with-mssql=/usr/local/freetds
$]make
$]su root
$]make install
$]make clean
If php is already installed and mssql not added follow run the below command
$yum install php-mssql
Step 3: FreeTDS configuration
$]vi /etc/freetds.conf (any editor available can used for this)
Go to the end of the file
Add the following lines
;--- Custom MSSQL server name ---
; THIS CAN BE ANY NAMED SERVER REFERENCE YOU WANT TO CREATE
[SERVERNAME]
; This is the host name of the MSSQL server
host=HOSTNAME
; This is the port number to use
port=PORTNUMBER
; This is the TDS version to use for anything over Server 2000
tds version=8.0
Save the file and exit
$]vi /etc/ld.so.conf (Again, any editor should be able to do this)
Go to the end of the file
Add the following line:
/usr/local/freetds/lib
Save the file and exit
Step 4: Enabling MSSQL PHP extension
$]cd /etc/php.d
If there is no file named mssql.ini create one
Edit the file and enter the following lines if not already there
; Enable mssql extension module
extension=mssql.so
Save the file and exit
Step 5: Apache (This should be done any time PHP changes)
$]/etc/rc.d/init.d/httpd stop
$]/etc/rc.d/init.d/httpd start
NOTE: $]apachectl -k graceful might work as well, though httpd start offers a status flag
Step 6: Test a file at the CLI and at the Web Server
I used the below code and got the result "Version: Microsoft SQL Server 2005 - 9.00.5000.00 (Intel X86) Dec 10 2010 10:56:29 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)"
< ?php
$server = 'wic018q.server-sql.com:4656';
// Connect to MSSQL
$link = mssql_connect($server, 'vs328460_1_dbo', 'Uq9y6hz3QX');
if (!$link) {
die('Something went wrong while connecting to MSSQL'.mssql_get_last_message());
}
$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);
echo "
Version: ".$row[0];
// Clean up
mssql_free_result($version);
? >
Please visist the below url for more details
http://www.robert-gonzalez.com/2008/03/31/connecting-php-on-linux-to-mssql-on-windows/
0 Responses to Connect to msssql server from linux server using php
Something to say?