既有系統使用 Windows 2000 + SQL Server 2000,需在 Linux + Apache + PHP 環境下讀寫該資料庫。Windows 下可用 ODBC 或 SQL Server Client,Linux 則需自行安裝 FreeTDS。
ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/freetds-0.53.tgzcp freetds-0.53.tgz /tmp/
cd /tmp
tar zxvf freetds-0.53.tgz
cd freetds-0.53
./configure --prefix=/usr/local/freetds --with-tdsver=7.0
gmake
gmake install
關鍵參數: --with-tdsver=7.0 指定 TDS 7.0 協定(對應 SQL Server 7.0/2000,預設 5.0 會用 4000 埠而非 1433)。
./configure --with-apxs --with-mysql ... --with-sybase=/usr/local/freetds
make
make install
注意參數是 --with-sybase 而非 --with-mssql。
vi /usr/local/freetds/etc/freetds.conf
典型設定:
[sqlserver]
host = 192.168.1.100 # SQL Server IP 或主機名
port = 1433
tds version = 7.0
;extension=mssql70.so
↓
extension=mssql70.so
$link = mssql_connect('sqlserver', 'username', 'password') or die('無法連線資料庫');
echo $link; // 顯示連線資源 ID 即成功
主機名稱對應 freetds.conf 中的區塊名稱(如 sqlserver)。
Call to undefined function mssql_connect(),檢查 PHP 編譯參數與 php.ini。freetds.conf,取消註解 dump file = /tmp/freetds.log,重現錯誤後查看日誌。mssql_* 函數族群。