2012-07-10 76 views
4

我在從PHP連接到MS SQL 2005時出現問題。連接到ms sql 2005從php使用freetds在centos上

我能夠從殼連接,使用...

tsql -S 10.0.0.134 -p 1433 -U gareth 

輸入一個簡單的查詢按預期工作...

1> SELECT @@VERSION AS MSSQL_VERSION 
2> go 
MSSQL_VERSION 
Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
Nov 24 2008 13:01:59 
Copyright (c) 1988-2005 Microsoft Corporation 
Express Edition on Windows NT 6.1 (Build 7601: Service Pack 1) 

但是,從PHP腳本試圖這樣做不工作...

$test = mssql_connect('10.0.0.134:1433', 'gareth', 'mypass'); 

...併產生一個mssql_connect()[function.mssql-connect]:無法連接到服務器錯誤。

我可以在/ usr/lib/php/modules中看到mssql.so模塊,phpinfo()顯示模塊已加載。

我很樂意使用odbc_connect代替,如果有人能告訴我一個例子配置freetds.conf和odbc.conf

感謝

+1

僅供參考:請不要使用'mysql_ *'函數獲取新代碼。 他們不再維護,社區已開始[棄用流程](http://goo.gl/KJveJ)。 請參閱[**紅框**](http://goo.gl/GPmFd)? 相反,您應該瞭解[準備語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net) /庫MySQLi)。 如果你不能決定,[這篇文章](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這是一本很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-10 15:52:37

回答

5

這是我能找到的關於PHP.net您的問題。也許它會幫助你解決它。

This might be obvious to some, but here is a quick tidbit that might save you some time if you are using FreeTDS in Linux: 
Be sure that you have these two lines in freetds.conf: 
dump file = /tmp/freetds.log 
dump file append = yes 

so you can tail -f it in the background of debugging the problem. This helped me find my issue on on CentOS Linux: 

1) tsql test works 

2) php-mssql connection in php also works WHEN RUN FROM THE SHELL 

3) running PHP through apache does NOT work. 

my /tmp/freetds.log file told me: 
net.c:168:Connecting to MYDBSERVER port MYDBPORT 
net.c:237:tds_open_socket: MYDBSERVER:MYDBPORT: Permission denied 

and the answer was my firewall/SELinux was denying the Apache processes access to connect to the remote MSSQL DB port, but my shell accounts were fine. 
+0

謝謝!調試非常有幫助,我發現SELinux會導致這個問題。 – garethmurton 2012-07-10 11:26:49

+3

我有同樣的問題,這個回覆幫了我:謝謝!在我的情況下,我簡單地解決了: 'setsebool -P httpd_can_network_connect 1' 這裏解釋:[comments.gmane.org/gmane.comp.db.tds.freetds/8627](http://comments.gmane.org /gmane.comp.db.tds.freetds/8627) – deste 2013-01-08 08:05:35