Linux

Linux下php加装mssql模块

在linux 下的php连接mssql,就需要安装第三方的freetds了。需要的朋友可以参考下。

1、安装配置freetds

wget http://mirrors.xmu.edu.cn/ubuntu/archive/pool/main/f/freetds/freetds_0.82.orig.tar.gz 
tar zxf freetds_0.82.orig.tar.gz 
cd freetds_0.82 
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 -–enable-msdblib -–enable-dbmfix -–with-gnu-ld -–enable-shared -–enable-static 
make && make install 

 2、编译php的mssql模块 

cd /path/to/php/source 进入PHP源码目录 
cd ext/mssql 进入MSSQL模块源码目录 
/usr/local/webserver/php/bin/phpize 生成编译配置文件 
./configure –with-php-config=/usr/local/webserver/php/bin/php-config –with-mssql=/usr/local/freetds 
make 
make install 
编译完成生成 mssql.so,修改php.ini,将该模块载入: 
extension=”/path/to/extension/mssql.so” 

 3、配置mssql 

cd /usr/local/freetds/etc 
编辑文件: 
vi freetds.conf 
[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 = UTF-8 #加入 
#加入 
[Server2005] 
host = 192.168.x.x 
port = 1433 
tds version = 7.2 

 4、测试php连接mssql 

  1. <?php  
  2. try {  
  3.     $hostname = '128.x.x.x'// 注意,这里和上面不同,要直接用IP地址或主机名  
  4.     $port = 1433; // 端口  
  5.     $dbname = "user"// 库名  
  6.     $username = "database"// 用户  
  7.     $pw = "passwd"// 密码  
  8.     $dbh = new PDO ( "dblib:host=$hostname:$port;dbname=$dbname""$username""$pw" );  
  9. } catch ( PDOException $e ) {  
  10.     echo "Failed to get DB handle: " . $e->getMessage () . "n";  
  11.     exit ();  
  12. }  
  13. echo 'connent MSSQL succeed';  
  14. $stmt = $dbh->prepare ( "select * from users" );  
  15. $stmt->execute ();  
  16. while ( $row = $stmt->fetch () ) {  
  17.     print_r ( $row );  
  18. }  
  19. unset ( $dbh );  
  20. unset ( $stmt );  
  21. ?>  

Publish Comment发表评论

点击刷新验证码 点击图片可刷新验证码

Comment网友评论

詹绍乾 Jancy © 版权所有 2020

Copyright © 2010 by zhansq.cn All right reserved.