1 頁 (共 1 頁)

linux底下使用freetds 連入mssql

發表於 : 2014-10-04, 22:05
tony
因為我用 php 5.2.x 版的
所以先重裝一次 http://z-win.blogspot.com/2010/05/ubuntu-php-52x.html
最後加上
sudo apt-get install freetds-bin
sudo aptitude install -t karmic php5-sybase
#改freetds 設定(這是我自已要用的)
sudo vim /etc/freetds/freetds.conf
#在[global]加上
text size = 104857600 #大小能到100b
client charset = UTF-8 #預設utf-8
#改mssql_connect的語言
sudo vim /etc/php5/apache2/php.ini
#加入
mssql.charset = "UTF-8"
sudo /etc/init.d/apache2 restart
#test方法
tsql -H mssql.ip -p 1433 -U username
#輸入password後出現 1> 就成功了
#php



#也可以用 匿名主機 方式連入,但mssql 要sp4以上
sudo vim /etc/freetds/freetds.conf
#加入
[mssql_con1]
host = this.con1.server
port = 1433
tds version = 8.0

#test方法
tsql -S mssql_con1 -U username
#輸入password後出現 1> 就成功了

#php測試
<?php
echo mssql_connect('mssql_con1', 'username', 'password')
? 'success'
: 'failure';
?>
當mssql的資料是 uniqueidentifier 在LINUX上的FREETDS 要用 mssql_guid_string 或 在SQL 用 cast(欄位名 as varchar(36)) 轉換字元, 在windows用com 時會自動轉
uniqueidentifier 的說明:
http://technet.microsoft.com/zh-tw/libr ... 90215.aspx
php的其它解法可以看:
http://uk.php.net/manual/en/function.ms ... string.php


如果發生:
Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This ensures consistent query semantics. Enable these options and then reissue your query.
請使用底下語法:

SET ANSI_WARNINGS ON
SET ANSI_NULLS ON

參考:
http://social.msdn.microsoft.com/Forums ... 38ad158a62