ubuntu php 连接sql server

1.下载最新的freetds ,访问 http://www.freetds.org/, 或者在 ubuntu上用 wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz 下载稳定的版本。

2.安装freetds 和 配置 freetds

cd 进入freetds所在目录,

$ tar zxvf freetds-stable.tgz(解压)
$ ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib

$ make 
$ make install

configure 步骤和 make 步骤可以用 普通用户的权限来完成,install 步骤最好用root 用户,或者是对/usr/local/ 目录有读写的权限的用户.

配置:

编辑/etc/ld.so.conf,在其中插入一行:
/usr/local/freetds/lib
然后运行以下指令使更改生效:
ldconfig

编辑freetds 的配置文件,配置数据库连接信息

vim /usr/local/freetds/etc/freetds.conf 内容如下:

"/usr/local/freetds/etc/freetds.conf" 40L, 1126C written
nvr@ubuntu:~$ sudo vim  /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 = 7.1


        # 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


# A typical Sybase server
#[egServer50]
        #host = symachine.domain.com
        #port = 5000
        #tds version = 5.0


# A typical Microsoft server
[egServer70]
        host = 115.28.239.229
        port = 1433
        tds version =7.1
        client charset=utf8

请注意绿色修改的部分

在这里找到egServer70节点,配置你数据所在的服务器地址,一般端口不用改,除非数据库访问用的是其它端口。注意一般这个配置文件中是没有 client charset 的选项的,

需要手动添加,防止数据库乱码。这里设置成 utf8 , 这个编码要和数据库编码一致。修改完成之后就保存退出。然后验证这个配置是否是ok 的。

cd /usr/local/freetds/bin/

./tsql -H 115.28.239.229 -p 1433 -U 用户名 -P 密码 -D 数据库

注意你的密码有特殊字符例如 !和 # 之类的,那么需要加上转义符 !# ,这样就会被 freetds 识别。

如果执行该命令返回的是 如下信息

Unexpected EOF from the server

那说明是freetds 的版本信息不正确,关于freetds 版本的信息 可以参考 http://www.freetds.org/userguide/choosingtdsprotocol.htm

那么对应的版本信息修改只需要在 /usr/local/freetds/etc/freetds.conf 中修改 特定数据库连接的tds version 修改内容的绿色部分

.php 连接 SQLserver

上面的测试直接用freetds 测试数据库是否可以链接数据库,那么怎么才能让php也支持SqlServer 呢? 这里需要用到php5的开发者模式,之前我默认只安装了php5,

所以这里你需要安装一下php5-dev ,直接用命令 apt-get install php5-dev,安装完成后在/usr/bin 目录下就会看到 phpize5 等相关php 开发这模式的插件

同样也可以用 wget 下载。下载后用 tar xf php-5.5.9.tar.gz ,

wget http://us2.php.net/get/php-5.5.9.tar.xz/from/this/mirror

这个地方下载下来可能是mirror 那么用 tar xf mirror

进入到 ext/mssql 目录执行以下命令:

/usr/bin/phpize5

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

sudo make install

 

记得make install 要用sudo 命令

(注意:php-config5 可能各个系统存放的位置不一样,请使用sudo find -name php-config5 来查找目录 freetds 类似 )

安装成功后,你可以在 /usr/lib/php5/20121206 这目录下有 mssql.so ,当然这个文件的大体目录是位于 /usr/lib/php5 ,至于后面有年月日组成的这个文件夹,在不同不服务器上是不一样的。

接下来就编辑php.ini 配置文件 /etc/php5/apache2/php.ini

在文件中加上 extension=mssql.so

然后重启 apache  service apache2 restart

测试:

在/var/www/html 目录下新建 index.php 文件

echo phpinfo();

进一步测试代码:

header("Content-type: text/html; charset=utf-8");  
$msdb=mssql_connect("115.28.239.22:1433","sa","");  
if (!$msdb) {  
        echo "connect sqlserver error";  
        exit;  
}  
mssql_select_db("drupaldb",$msdb);  
$result = mssql_query("SELECT  * FROM test", $msdb);  
while($row = mssql_fetch_array($result)) {  
        var_dump($row);  
}  
mssql_free_result($result); 

乱码 在/usr/local/freetds/etc/freetds.conf 下,添加

client charset = UTF-8

原文地址:https://www.cnblogs.com/DoNetCShap/p/8709563.html