1.准备软件
php-7.1 下载地址:http://php.net/downloads.php
unixodbc-2.3.4 下载地址:http://www.unixodbc.org/
freetds 下载地址:http://www.freetds.org/index.html
2. 优先安装unixodbc
tar zxf unixODBC-2.3.4
cd unixODBC-2.3.4
./configure --prefix=/usr/local/unixODBC
make && make install
3.安装freetds
tar zxf freetds-patched.tar.gz
cd freetds-1.00.24
./configure --with-tdsver=7.4 --prefix=/usr/local/freetds --enable-msdblib --with-gnu-ld --enable-shared --enable-static --with-unixodbc=/usr/local/unixODBC
make && make install
(备注:以上编译参数如果需要了解请用 ./configure -h )
(编译如果出现:configure: error: sql.h not found ,可参考 安装freetds报错sql.h not found这篇文章处理)
4.配置
①:先配置freetds
配置文件:/usr/local/freetds/etc/freetds.conf
![](https://i-blog.csdnimg.cn/blog_migrate/551ff944d766baf188b10ca924e722c6.png)
如图上图所示:配置了sql2005
测试是否可以连接
/usr/local/freetds/bin/tsql -S sql2005 -U sa -P 123456
![](https://i-blog.csdnimg.cn/blog_migrate/b7730e1a4a20c1a8989c41ff0fec1b21.png)
如图上图所示则说明连接sql server 2005 数据库成功
(这条可以忽略)也可以直接连接,如下图所示:
/usr/local/freetds/bin/tsql -H 192.168.0.31 -p1433 -U sa -P 123456
![](https://i-blog.csdnimg.cn/blog_migrate/76d74feeddf3308f38bf169dc6420e23.png)
这种方式会提示字符集问题
②:配置unixODBC
a) 配置odbcinst.ini
配置文件 /usr/local/unixODBC/etc/odbcinst.ini
![](https://i-blog.csdnimg.cn/blog_migrate/a36d7c80180f56d4abd23e6661566061.png)
b) 配置odbc.ini
配置文件 /usr/local/unixODBC/etc/odbc.ini
![](https://i-blog.csdnimg.cn/blog_migrate/81bb0a5a299d837d05054d1790627f9f.png)
(说明:sql2005 这个就是freetds.conf配置的)
c) 测试
/usr/local/unixODBC/bin/isql -v sql2005dsn sa 123456
![](https://i-blog.csdnimg.cn/blog_migrate/8a6ae3b68f713335cfe05695e812c892.png)
执行sql 语句结果
![](https://i-blog.csdnimg.cn/blog_migrate/1aaf3d568b3eacf8bf2bdf10e16a712f.png)
5) 安装php
(a)如果已经安装了php,只需要安装pdo-odbc和pdo-dblib扩展即可
例如:编译dblib 扩展
/usr/local/php/bin/phpize
./configure --with-pdo-dblib=/usr/local/freetds/ --with-php-config = /usr/local/php/bin/php-config
make && make install
配置文件添加编译好的扩展
(b) 安装php-7.1.0
./configure --prefix=/usr/local/php710 --with-config-file-path=/usr/local/php710/etc --with-mcrypt=/usr/include --with-gd --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-freetype-dir --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --with-pcre-regex --with-curl -with-jpeg-dir --with-png-dir --enable-session --with-mcrypt --with-iconv --with-unixODBC=/usr/local/unixODBC --with-pdo-odbc=unixODBC,/usr/local/unixODBC --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client/lib --with-pdo-oci=instantclient,/usr,11.2 --enable-exif --enable-ftp --with-mhash --enable-soap --enable-zip --with-pdo-dblib=/usr/local/freetds/
make && make install
(pdo-oci 和 oci 扩展是用来连接 orcale 数据,如果没有配置好环境,可以去掉这两项,否则编译不过去,还有编译不过去,请自行百度)
查看是否扩展编译成功
![](https://i-blog.csdnimg.cn/blog_migrate/e8282baa91a7769b691720d40d2ad812.png)
(6) PHP 连接测试
(a) odbc 连接方式
![](https://i-blog.csdnimg.cn/blog_migrate/a434a2e3ab875cca4fce043ae9e897a1.png)
(说明:sql2005dsn 这个就是 odbc.ini 配置的)
(b) dblib 连接方式
![](https://i-blog.csdnimg.cn/blog_migrate/baca59d38f665f63977bf88003e15c3c.png)
(c) 两种方式运行的结果
![](https://i-blog.csdnimg.cn/blog_migrate/c05c123601f441d315197d638028b7be.png)
(说明:页面编码是GBK)
7.推荐medoo类库,网址:http://medoo.lvtao.net/,可以轻易连接各类数据库。
8.链接sybase
8.1. freetds配置
![](https://i-blog.csdnimg.cn/blog_migrate/7050249ad7ec0fd5f25561013406211c.png)
(备注:如果 tds version 设置不对,是会报错的,可根据情况设置版本。错误如下图)
![](https://i-blog.csdnimg.cn/blog_migrate/3e5f4c142d8770a192b8b09bdd51a24c.png)
8.2 odbc.ini配置
![](https://i-blog.csdnimg.cn/blog_migrate/369084cbec8545f078a8c375820dde1b.png)
8.3 php连接
![](https://i-blog.csdnimg.cn/blog_migrate/a703edf964dea3bc1fdda940cc395ea9.png)
(dblib 连接 一定要设置对 tds version,否则链接不了,注释部分是odbc连接,两种运行结果是一致)
(dblib:host=192.168.0.33:5000;dbname=app;charset=GBK;version=4.2,这样连接也可以,就不用在freetds.conf 中 golobal 设置 tds version = 4.2)