標籤:
由於工作原因我們需要通過php訪問我們以前的Sql Server 2005資料,所以就有了這篇文章的誕生.廢話就少說了,做程式設計的最不喜歡兜圈子了.用簡介步驟說明問題,往下看.
系統: Linux
資料庫: Sql Server 2005 1.下載FreeTDS
官方網站: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 其他可選 根據自己情況
--enable-dbmfix --with-gnu-ld --enable-shared --enable-static安裝freetds到目錄/usr/local/freetds:--prefix=/usr/local/freetds 如果不帶這個預設好像也是這目錄 對應資料庫版本--我的是Microsoft SQL Server 2005 所以我帶的是 --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#這個名字程式和命令列用得上,叫什麼自己定
[Server2005]
host = 192.168.3.100 #我的SQL Server2005 IP,根據自己改
port = 1433
tds version = 7.2
4.測試連接:
[[email protected] 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>
出現這個表示串連成功! 退出:quit 和 exit 都行. 參數說明
-S 配置的服務名
-H 主機名稱
-p 連接埠
-U username
-P password
-D database
5.測試查詢:
# ./tsql -S Server2005 -p 1433 -U java -P java -D PublicDB1> select USER_ID,TRUE_NAME from USER_INFO
2> go可以顯示中文沒問題! 6.讓php支援mssql(freeTDS)
重新編譯php 這些參數根據自己情況來定,下面是我們需要的
但是必須帶--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 如果編譯報錯請執行:
# touch /usr/local/freetds/include/tds.h
# touch /usr/local/freetds/lib/libtds.a 7.php測試程式<?php
/**
* MOIT
*
* @author 明白([email protected]) 日 期: Wed Nov 18 05:00:07 GMT 2009
* @copyright Copyright (c) 2009
* @desc 測試 */ $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‘] . "<br>\n";
}
?> 8.我還碰到需要轉碼問題$author = iconv(‘utf-8‘,‘gb2312‘,$row[‘TRUE_NAME‘]);//貼子的發表者的會員名稱utf-8轉成gb2312 9.安裝完畢,祝您成功!
Linux下用freetds串連mssql中文亂碼的問題【參考1】