基于Linux下PHP连接SQL Server的FreeTDS配置

开发 后端
CentOS 5.4 Linux 下的 PHP(FastCGI) 需要连接相关部门的SQL Server 2000数据库,配置了扩展FreeTDS扩展。

  CentOS 5.4 Linux 下的 PHP(FastCGI) 需要连接相关部门的SQL Server 2000数据库,配置了扩展FreeTDS扩展。

  1、编译安装FreeTDS

mkdir -p /data0/software/  
cd /data0/software/  
wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz  
tar zxvf freetds-stable.tgz  
cd freetds-0.82/  
./configure --prefix=/usr/local/webserver/freetds --with-tdsver=8.0 --enable-msdblib  
make && make install  
cd ../  
 
echo "/usr/local/webserver/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf  
ln -s /usr/local/webserver/freetds/lib/libsybdb.so.5.0.0 /usr/local/webserver/freetds/lib/libsybdb.so.4  
/sbin/ldconfig  
 
rm -f /usr/local/webserver/freetds/etc/freetds.conf  
vi /usr/local/webserver/freetds/etc/freetds.conf  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

  输入以下内容:

  引用

[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 
 
        host = mssql.yourdomain.com  
        port = 1433 
        tds version = 8.0  
        client charset = UTF-8 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

  2、编译安装PHP自带MSSQL扩展

  进入本地已存在的php-5.2.XX源码包目录:

cd /data0/software/php-5.2.XX/ext/mssql/  
/usr/local/webserver/php/bin/phpize  
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-mssql=/usr/local/webserver/freetds/  
make && make install  
  • 1.
  • 2.
  • 3.
  • 4.

  3、在php.ini配置文件中增加mssql.so

vi /usr/local/webserver/php/etc/php.ini  
  • 1.

  增加一行:

  引用

extension = "mssql.so" 
  • 1.

  4、重启PHP FastCGI

/usr/local/webserver/php/sbin/php-fpm restart  
  • 1.

  5、测试文件(test_mssql.php):
 

php     
header("Content-type: text/html; charset=utf-8");     
$msdb=mssql_connect("mssql.yourdomain.com:1433","username","password");     
if (!$msdb) {     
        echo "connect sqlserver error";     
        exit;     
}     
mssql_select_db("database_name",$msdb);     
$result = mssql_query("SELECT top 5 * FROM table", $msdb);     
while($row = mssql_fetch_array($result)) {     
        var_dump($row);     
}     
mssql_free_result($result);     
?>    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

【编辑推荐】

  1. PHP中发现奇异Bug 导致Web服务器崩溃
  2. 解析PHP与SQL注入攻击
  3. PHP生成随机字符串的三种方法
责任编辑:陈贻新 来源: 张宴的博客
相关推荐

2011-03-09 10:50:27

Linuxapachephp

2010-09-14 13:08:11

配置无线连接

2010-11-08 16:56:52

SQL Server绑

2010-11-08 17:01:53

SQL Server绑

2010-11-10 10:09:40

2011-03-08 11:11:38

LinuxLAMP安装

2011-03-08 11:11:38

LinuxLAMP安装

2010-11-08 15:47:01

SQL Server外

2010-11-11 13:51:36

SQL Server内

2010-07-22 10:52:21

SQL Server数

2016-11-21 13:35:04

LinuxSQL Server微软

2011-03-29 13:10:56

SQL Server

2010-11-08 14:40:46

SQL Server连

2010-11-08 15:21:17

SQL Server连

2009-08-31 09:23:34

SQL Server 云计算

2010-10-20 17:37:23

SQL Server连

2023-08-02 10:58:18

SP_WHOSQL Server

2009-11-26 09:38:18

Suse Linux

2009-04-16 17:34:19

2009-06-14 16:59:16

ibmdwWebSphere
点赞
收藏

51CTO技术栈公众号