由于工作原因我們需要通過
PHP訪問我們以前的Sql Server 2005數(shù)據(jù),所以就有了這篇文章的誕生.廢話就少說了,做程序設(shè)計(jì)的最不喜歡兜圈子了.用簡(jiǎn)介步驟說明問題,往下看.
系統(tǒng): Linux
數(shù)據(jù)庫: Sql Server 2005
1.下載FreeTDS
官方網(wǎng)站:http://www.freetds.org
2.安裝FreeTDS
# tar zxvf freetds-current.tgz(解壓)
# ./configure --prefix=/usr/local/freetds --with-tdsver=7.2 --enable-msdblib
# make
# make install
其他可選 根據(jù)自己情況
--enable-dbmfix --with-gnu-ld --enable-shared --enable-static
安裝freetds到目錄/usr/local/freetds:--prefix=/usr/local/freetds 如果不帶這個(gè)默認(rèn)好像也是這目錄
對(duì)應(yīng)數(shù)據(jù)庫版本--我的是Microsoft SQL Server 2005 所以我?guī)У氖?--with-tdsver=7.2
4.2 Sybase SQL Server < 10 and Microsoft SQL Server 6.5
5.0 Sybase SQL Server >= 10
7.0 Microsoft SQL Server 7.0
7.1 Microsoft SQL Server 2000
7.2 Microsoft SQL Server 2005
3.編輯/usr/local/freetds/etc/freetds.conf
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting "text size" to a more reasonable limit
text size = 64512
#解決中文亂碼問題
client charset=utf8
# A typical Sybase server
#[egServer50]
# host = symachine.domain.com
# port = 5000
# tds version = 5.0
# A typical Microsoft server
#[egServer70]
# host = ntmachine.domain.com
# port = 1433
# tds version = 7.0
#這個(gè)名字程序和命令行用得上,叫什么自己定
[Server2005]
host = 192.168.3.100 #我的SQL Server2005 IP,根據(jù)自己改
port = 1433
tds version = 7.2
4.測(cè)試連接:
[root@test bin]# ./tsql -S Server2005 -p 1433 -U java -P java -D PublicDB
locale is "zh_CN"
locale charset is "GB2312"
Default database being set to PublicDB
1>
出現(xiàn)這個(gè)表示連接成功! 退出:quit 和 exit 都行.
參數(shù)說明
-S 配置的服務(wù)名
-H 主機(jī)名
-p 端口
-U username
-P password
-D database
5.測(cè)試查詢:
# ./tsql -S Server2005 -p 1433 -U java -P java -D PublicDB
1> select USER_ID,TRUE_NAME from USER_INFO
2> go
可以顯示中文沒問題!
6.讓PHP支持mssql(freeTDS)
重新編譯PHP 這些參數(shù)根據(jù)自己情況來定,下面是我們需要的
但是必須帶--with-mssql=/usr/local/freetds
./configure --prefix=/usr/local/PHP --with-MySQL=/usr/local/MySQL --with-apxs2=/usr/local/apache/bin/apxs --with-gd=/usr/local/gd --with-jpeg-dir=/usr/local/libjpeg --with-png-dir=/usr/local/libpng --with-zlib-dir=/usr/local/zlib --with-libxml-dir=/usr/local/libxml2 --with-iconv=/usr/local/libiconv --with-freetype-dir=/usr/local/freetype --with-pdo-MySQL=/usr/local/PHPbak/lib/PHP/extensions/no-debug-non-zts-20060613/pdo_MySQL.so --enable-sockets --with-curl --with-pear --with-mssql=/usr/local/freetds
如果編譯報(bào)錯(cuò)請(qǐng)執(zhí)行:
# touch /usr/local/freetds/include/tds.h
# touch /usr/local/freetds/lib/libtds.a
7.PHP測(cè)試程序
/**
* MOIT
*
* @author 明白(admin126com@126.com) 日 期: Wed Nov 18 05:00:07 GMT 2009
* @copyright Copyright (c) 2009
* @desc 測(cè)試
*/
$msconnect=mssql_connect("Server2005","java","java");
$msdb=mssql_select_db("PublicDB",$msconnect);
$msquery = "select TRUE_NAME,USER_ID,USER_NAME,PASSWORD from USER_INFO";
$msresults= mssql_query($msquery);
while ($row = mssql_fetch_array($msresults)) {
echo $row["USER_ID"] . " ".$row["TRUE_NAME"]. " " . $row["USER_NAME"] . " " . $row["PASSWORD"] . "
";
}
?>
8.安裝完畢,祝您成功!