扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
由于工作原因我们需要通过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.测试连接:
[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>
出现这个表示连接成功! 退出:quit 和 exit 都行.
参数说明
-S 配置的服务名
-H 主机名
-p 端口
-U username
-P password
-D database
5.测试查询:
# ./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 这些参数根据自己情况来定,下面是我们需要的
但是必须带--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测试程序
/**
* MOIT
*
* @author 明白(admin126com@126.com) 日 期: 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'] . "
\n";
}
?>
8.安装完毕,祝您成功!
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者