Connect remotely to MSSQL with PHP

Issues related to software problems.
Posts: 5
Joined: 2011/01/11 03:54:30

Connect remotely to MSSQL with PHP

Postby Tenno » 2011/01/11 04:21:15

Hi , I'm a new member and sorry for my bad English.

I just succesfully installed Centos 5.5 64bit with following package:

- httpd 2.2.3-43
- php-mysql 5.1.6-27
- mysql 5.0.77-4

and all these packages are running ok.

My question is how do i connect from Centos to another MSSQL server using PHP?
I had read several article but cannot find the best way to achieve this because most of the instruction seems to be quite complicated.
I found somewhere in this forum that all it need just this simple command: "yum --enablerepo=extras install php-mssql freetds".
While others article said that i need to install "FreeTDS" and some ODBC driver.
I'm a little bit curious since most of that article are using an older version of Centos and PHP.
So im asking if anyone here know the simple and correct way of doing this or to share any guide about this.


Posts: 1015
Joined: 2006/06/10 18:27:44
Location: 32 4′N 34 47′E

Re: Connect remotely to MSSQL with PHP

Postby yyagol » 2011/01/11 06:48:21

Hi ,
all you need is php-mysql , and to make sure the mysql-servers on the far end
is open and accept connections from your server IP and user

Posts: 10642
Joined: 2005/08/05 15:19:54
Location: Northern Illinois, USA

Connect remotely to MSSQL with PHP

Postby gerald_clark » 2011/01/11 15:36:39

The question is not clear from the wording "another MSSQL".
Do you mean "another MySQL" or "an MSSQL" ?

Posts: 5
Joined: 2011/01/11 03:54:30

Re: Connect remotely to MSSQL with PHP

Postby Tenno » 2011/01/12 00:12:36

Sorry if my question is not clear enough.
I want to connect to Microsoft SQL Server (Windows) from my Centos server, using PHP.
There is some data that i need from the Microsoft SQL database (remote server) and then insert it in MySQL (Centos) database.
I hope someone here can shed some light on it.
Thanks a lot.

User avatar
Forum Moderator
Posts: 22599
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Connect remotely to MSSQL with PHP

Postby TrevorH » 2011/01/12 00:43:52

There is certainly a php-mssql package available in the extras repository so just a `yum install php-mssql` should sort that out - it pulls in freetds as a dependency too. If you already have those installed but it doesn't seem to be connecting, the one possibility is that you are being blocked by SELinux. To check that run

Code: Select all

# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> on
httpd_can_network_connect_db --> on

and to enable them if they are not on, do

Code: Select all

setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1

Posts: 5
Joined: 2011/01/11 03:54:30

Re: Connect remotely to MSSQL with PHP

Postby Tenno » 2011/01/12 01:59:31

Thanks a lot TrevorH, i will try that out and post back the result.
By the way, how do i test if the connection was successful using PHP script?
And should i use "mssql_connect" or "odbc_connect" in the PHP connection script?
Sorry for asking too much question.

p/s: by the way, SELinux is disable on my machine.

Posts: 7
Joined: 2010/03/23 22:15:07

Re: Connect remotely to MSSQL with PHP

Postby fonya » 2011/01/12 21:11:37

For mssql You must use mssql_connect. /or sybase, or freetds, if I do not mistake too much 8-)/
If You want to use mssql_connect, You need to install php-mssql from extras repository:
yum install --enablerepo=extras php-mssql
and restart httpd:
service httpd restart

That's it :-)

Posts: 5
Joined: 2011/01/11 03:54:30

Re: Connect remotely to MSSQL with PHP

Postby Tenno » 2011/01/13 03:03:23

Thanks Fonya for your reply.
Same as TrevorH before, your suggestion is to do "yum install php-mssql" which i already did. That command also install FreeTDS for me.

And now i just found an article/guide about this at ... r-php.html
According to that article, that "yum install php-mssql" is just at a first step.
There is more step such as "yum install freetds freetds-devel unixODBC unixODBC-devel php-devel" and editing several file.
So my question, is all that further step really necessary? Or the first command that you and TrevorH suggested is enough?

I really no expert so i need to know all the consequences before start doing this.
Thanks a lot.

Posts: 114
Joined: 2005/09/09 05:30:18

Re: Connect remotely to MSSQL with PHP

Postby teleport » 2011/01/13 10:44:42


This is my working config:

Centos 5.5, freetds-0.82, php-mssql-5.1.6-15.el5.centos.1, currently works against 32bit SQL 2000 server. Development machine is same config, working against 64bit SQL 2008 Server.


Code: Select all

        text size = 264512

        host =
        client charset = UTF-8
        tds version = 8.0


Code: Select all

#       date format = %b %e %Y %I:%M:%S:%z%p
        date format = %b %e %Y %I:%M:%S %p

#       date format = %b %e %Y %I:%M:%S:%z%p
        date format = %b %e %Y %I:%M:%S %p
        language = us_english
        charset = iso_1

php connection:

Code: Select all

$msdb = mssql_pconnect("pdc\\database",  "user",  "pass");

Hope this helps.

Posts: 5
Joined: 2011/01/11 03:54:30

Re: Connect remotely to MSSQL with PHP

Postby Tenno » 2011/01/18 02:15:37

Thanks teleport for your help but it didn't work.
Are you sure that freetds.conf the only thing that need to be configured?
No need for ODBC or something?

Here is my /etc/freetds.conf
# A typical Microsoft SQL Server 2000 configuration
host =
port = 1433
tds version = 8.0

And here the PHP script that i use to test the connection.
$db = mssql_connect("","sa","") or die("Unable to connect to server");
echo "

Here is the list of relevan application package i got in my machine. All package are installed together during Centos installation except for PHP-MSSQL and FreeTDS which i use 'yum' as other member suggested.




So is there anything else that im missing?
I really need this thing working by the end of this week.
Anyone please help.