Connect remotely to MSSQL with PHP

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

Connect remotely to MSSQL with PHP

Post by 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.

Thanks.

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

Re: Connect remotely to MSSQL with PHP

Post by 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

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

Connect remotely to MSSQL with PHP

Post by 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" ?

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

Re: Connect remotely to MSSQL with PHP

Post by 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
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Connect remotely to MSSQL with PHP

Post by 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]
# getsebool -a | grep httpd_can_network_connect
httpd_can_network_connect --> on
httpd_can_network_connect_db --> on
[/code]

and to enable them if they are not on, do

[code]
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
[/code]

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

Re: Connect remotely to MSSQL with PHP

Post by 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.

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

Re: Connect remotely to MSSQL with PHP

Post by 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 :-)

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

Re: Connect remotely to MSSQL with PHP

Post by 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 http://almamunbd.blogspot.com/2010/06/how-to-install-ms-sql-extension-for-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.

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

Re: Connect remotely to MSSQL with PHP

Post by teleport » 2011/01/13 10:44:42

Hello,

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.

/etc/freetds.conf
[code][global]
text size = 264512

[pdc]
host = 192.168.11.10
client charset = UTF-8
tds version = 8.0
[/code]

/etc/locales.conf
[code][default]
# date format = %b %e %Y %I:%M:%S:%z%p
date format = %b %e %Y %I:%M:%S %p

[en_US]
# 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
[/code]

php connection:
[code]$msdb = mssql_pconnect("pdc\\database", "user", "pass");
[/code]

Hope this helps.

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

Re: Connect remotely to MSSQL with PHP

Post by 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
[quote]
# A typical Microsoft SQL Server 2000 configuration
[MyServer2k]
host = 10.100.20.4
port = 1433
tds version = 8.0
[/quote]

And here the PHP script that i use to test the connection.
[quote]
"."Thanks";
?>
[/quote]

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.
[quote]
php-mssql-5.1.6-15.el5.centos.1
php-5.1.6-27.el5
php-mysql-5.1.6-27.el5
php-pdo-5.1.6-27.el5
php-odbc-5.1.6-27.el5
php-ldap-5.1.6-27.el5
php-cli-5.1.6-27.el5
php-common-5.1.6-27.el5

httpd-manual-2.2.3-43.el5.centos
httpd-2.2.3-43.el5.centos
system-config-httpd-1.3.3.3-1.el5

freetds-0.64-11.el5.centos

php-odbc-5.1.6-27.el5
mysql-connector-odbc-3.51.26r1127-1.el5
[/quote]

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

Post Reply