擁抱自由,使用 linux !

在Linux底下真是太幸福了,有好多東西可以玩。想從windows解脫就是現在!

Sep-2-06

php connect to SQL2000

posted by Thomas

一次偶然的機會必須要使用以下奇怪的架構

Database Server
MS SQL 2000

Web Server
Linux using apache, php…

資料放在sql2000, 建立php 網頁存取

網路上查了一下資料大致作法如下

1.
安裝freetds
2.
重新安裝php

細節
Ubuntu (ref.1)

安裝連mssql的套件
sudo apt-get install freetds-dev
sudo apt-get install tdsodbc

修改/etc/freetds/freetds.conf
[FSData]
host = 192.168.x.x
port = 1433
client charset = cp950
tds version = 8.0

修改/etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS 0.61-5 Deb
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
FileUsage = 1
CPTimeout = 5
CPReuse = 5

修改/etc/odbc.ini
[Products]
Description = Products on Our MSSQL Server
Driver = FreeTDS
Servername = FSData
Database = Products

下載php source code
sudo apt-get source php5

修改php 資料夾 中debian/rules
加入於 common config
–with-mssql

重新建立deb檔
sudo apt-get build-dep php5
sudo dpkg-buildpackage

安裝
sudo dpkg -i php5_5*.deb

複製libphp.so
sudo cp ~/php5-*/apache2-build/libs/libphp5.so /usr/lib/apache2/modules/libphp5.so

重新啟動
sudo /etc/init.d/apache2 restart

如果全部用source code(ref.2)

FreeTDS 官方網站: http://www.freetds.org/
下載 FreeTDS source distribution
編譯參數: –prefix=/usr/local/freetds –enable-msdblib
修改 /etc/ld.so.conf, 加入 /usr/local/freetds/lib
再執行 ldconfig
修改 /usr/local/freetds/etc/freetds.conf
內容同上

重新編譯 PHP Source PHP
官方網站: http://www.php.net/
編譯前, 先用 php 程式執行 echo phpinfo();
顯示原本安裝時所有的參數, 以取用需要的 configure 參數
最後再加上 –with-mssql=/usr/local/freetds
執行範例:
./configure ‘–prefix=/usr/local/php_4.3.10′ ‘–localstatedir=/var’ ‘–disable-debug’ ‘–enable-pic’ ‘–disable-rpath’ ‘–enable-inline-optimization’ ‘–with-bz2′ ‘–with-db4=/usr’ ‘–with-curl’ ‘–with-exec-dir=/usr/bin’ ‘–with-freetype-dir=/usr’ ‘–with-png-dir=/usr’ ‘–with-gd’ ‘–enable-gd-native-ttf’ ‘–without-gdbm’ ‘–with-gettext’ ‘–with-ncurses’ ‘–with-gmp’ ‘–with-iconv’ ‘–with-jpeg-dir=/usr’ ‘–with-openssl’ ‘–with-png’ ‘–with-pspell’ ‘–with-regex=system’ ‘–with-xml’ ‘–with-expat-dir=/usr’ ‘–with-dom’ ‘–with-dom-xslt=/usr’ ‘–with-dom-exslt=/usr’ ‘–with-xmlrpc=shared’ ‘–with-pcre-regex=/usr’ ‘–with-zlib’ ‘–with-layout=GNU’ ‘–enable-bcmath’ ‘–enable-exif’ ‘–enable-ftp’ ‘–enable-magic-quotes’ ‘–enable-safe-mode’ ‘–enable-sockets’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–enable-track-vars’ ‘–enable-trans-sid’ ‘–enable-yp’ ‘–enable-wddx’ ‘–with-pear=/usr/share/pear’ ‘–with-imap=shared’ ‘–with-imap-ssl’ ‘–with-kerberos’ ‘–with-ldap=shared’ ‘–with-mysql’ ‘–with-pgsql=shared’ ‘–with-snmp’ ‘–with-snmp=shared’ ‘–enable-ucd-snmp-hack’ ‘–with-unixODBC’ ‘–enable-memory-limit’ ‘–enable-bcmath’ ‘–enable-shmop’ ‘–enable-calendar’ ‘–enable-dbx’ ‘–enable-dio’ ‘–enable-mcal’ ‘–enable-mbstring’ ‘–enable-mbstr-enc-trans’ ‘–enable-mbregex’ ‘–with-apxs2=/usr/sbin/apxs’ ‘–with-mssql=/usr/local/freetds’
編譯、安裝完成後, cp php.ini-dist /prefix/lib/php.ini

測試連線(ref.3)
mssql_connect(’FSData’,'username’,'password’);
//第一個參數是 servername, 參照 freetds.conf
mssql_select_db(’your_db’);

如此一來已經大功告成
要注意的是我有遇到sql2000定義 “ntext” 並不支援 請改成 “text” !!

reference:
ref.1 How to compile MSSQL support into PHP in Ubuntu Dapper Drake
ref.2 PHP@Linux vs MS-SQL Server database (using FreeTDS)
ref.3 PHP MSSQL extension

Tags: , ,