Linux 让PHP支持MSSQL

来源:互联网 发布:网络改变生活ppt 编辑:程序博客网 时间:2024/05/22 15:15

FreeTDS官方网站:http://www.freetds.org 当前版本0.82

wget http://mirrors.xmu.edu.cn/ubuntu/archive/pool/main/f/freetds/freetds_0.82.orig.tar.gz

1. 编译FreeTDS

# tar zxvf freetds-0.82.tar.gz

# cd freetds-0.82

//--with-tdsver=7.0是指安装tds 7.0版本(如果没有加这个参数,则按照默认编译为5.0。5.0连接数据库的端口是4000,而不是SQLServer的1433)

特别注意要加--with-tdsver 选项

具体可以看下什么作用

./configure --help | grep tdsver 


# ./configure --prefix=/usr/local/freetds --with-tdsver=7.0

# make

# make install

2. 编译成模块

//进入php的源码目录、MSSQL模块源码目录

# cd /home/wxw/gd/php-5.2.5/ext/mssql/

//生成编译配置文件

# /usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/

# make

# make install

//将mssql.so添加到php.ini,路径在安装后会有提示

# vim /usr/local/php/lib/php.ini

extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/mssql.so"


===========以上是网上找到的,下面是自己的=========================


php-5.2.17.tar.gz

tar -zxf freetds_0.82.orig.tar.gz

cd freetds-0.82/

./configure --prefix=/usr/local/freetds --with-tdsver=7.0

 make && make install && make clean

cd /usr/local/soft/php-5.2.17/ext/mssql

查看whereis phpize
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config --with-mssql=/usr/local/freetds/

 make && make install && make clean

 vi /usr/local/Zend/etc/php.ini

extension = "mssql.so"

最后
vi /usr/local/freetds/etc/freetds.conf

加入下面的

client charset = utf8  
[server2008]
        host = 192.168.18.101
        port = 1875
        tds version = 8.0


注,client charset = utf8  这行一定要放在上面,不然不生效,出现乱码。192.168.18.101为要连接的数据库地址



注意
//--with-tdsver=7.0是指安装tds 7.0版本(如果没有加这个参数,则按照默认编译为5.0。5.0连接数据库的端口是4000,而不是SQLServer的1433)
特别注意要加--with-tdsver 选项
具体可以看下什么作用 ./configure --help | grep tdsver

-----------------------------------------------------------------------------------------------

linux测试连接
 
./tsql -S 192.168.0.109 -P 1433 -U 用户名 -P 密码 -D 数据库
如果可以有看到1>说明连接成功
输入sql语句
1>select * from table;
2>go


php文件测试
<?php

//linux链接用端口用“:”,win用“,”否则链接不上
//MSSQL Server,注意一定要注明1433端口号,否则将无法连接
$hostname = "127.0.0.1,1875"; //win
//$hostname = "192.168.18.101:1875"; //linux

$dbuser = "sa"; //用户名
$dbpasswd = "123456"; //密码
$db_id = mssql_connect($hostname,$dbuser,$dbpasswd) or die("无法连接数据库服务器!");
$db = mssql_select_db("test",$db_id) or die("无法连接数据库!");
//执行查询语句
$query = "SELECT * FROM [test].[dbo].[user]";
$result = mssql_query($query);
while ( $row = mssql_fetch_assoc($result) ) {
    var_dump($row);
}
exit;



原创粉丝点击