linux 下 PHP 支持 mssql 的配置说明 freetds 并且解决乱码问题
ZDNet
软件频道 时间:2009-12-07 作者: | 周立峰的博客 我要评论()
本文关键词: 本文关键词:
PHP Linux MySQL 由于工作原因我们需要通过 php 访问我们以前的 Sql Server 2005 数据,所以就有了这篇文章的诞生.废话就 少说了,做程序设计的最不喜欢兜圈子了.用简介步骤说明问题,往下看.
由于
工作原因我们需要通过 PHP 访问我们以前的 Sql Server 2005 数据,所以就有了这 篇文章的诞生.废话就少说了,做程序
设计的最不喜欢兜圈子了.用简介步骤说明问题,往下看. 系统: Linux 数据库: Sql Server 2005 1.下载 FreeTDS
官方网站: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 # port = 5000 # tds version = 5.0 # A typical Microsoft server #[egServer70] # host = ntmachine.domain # 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
可以显示中文没问题!
configure 说明:--prefix=/usr/local/freetds 是指安装到/usr/local/freetds 这个目录中 --with-tdsver=8.0 是指安装 tds 8.0 版本(如果没有加这个参数,则按照默认编译为 5.0。5.0 连接数据库的端 口是 4000,而不是 SQLServer 的 1433)
如果是用 LINUXODBC 连时改如下: 1.先安装 ODBC: # tar vxzf unixODBC-2.2.8.tar.gz # cd unixODBC-2.2.8 # ./configure --prefix=/usr/local/unixODBC # make # make install 2.配置 FREETDS 时改成如下语句: # ./configure --prefix=/usr/local/freetds