1.下载freetds 并安装
1 2 3 4 5 | wget ftp://ftp.freetds.org/pub/freetds/current/freetds-current.tar.gz tar -zvxf freetds-current.tar.gz cd freetds-dev.0.99.479 ./configure --prefix=/usr/local/freetds --with-tdsver=7.2 --enable-msdblib --with-gnu-ld --enable-shared --enable-static make && make install |
2安装php5.3 mssql扩展
1 2 3 4 5 6 7 | wget http://cn2.php.net/distributions/php-5.3.29.tar.gz tar -zvxf php-5.3.29.tar.gz cd php-5.3.29/ext/mssql /www/wdlinux/nginx_php-5.3.29/bin/phpize //生成编译配置文件 ./configure --with-php-config=/www/wdlinux/nginx_php-5.3.29/bin/php-config --with-mssql=/usr/local/freetds make make install |
编译完成生成 mssql.so,修改php.ini,将该模块载入:
1 | extension=/path/to/extension/mssql.so |
3.编辑freetds.conf /usr/local/freetds/etc下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # $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 = 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 # A typical Sybase server [egServer50] host = symachine.domain.com port = 5000 tds version = 5.0 # A typical Microsoft server [egServer70] host = ntmachine.domain.com port = 1433 tds version = 7.0 [sql2008] host = ntmachine.domain.com port = 1433 tds version = 7.2 |
5.测试
1 | /usr/local/freetds/bin/tsql -S sql-server-2000 -U sa -P test |
出现
1 2 3 4 | locale is "zh_CN.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 1> |
6.成功
7.程序使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < ?php try { $hostname='218.x.x.x';//注意,这里和上面不同,要直接用IP地址或主机名 $port=1433;//端口 $dbname="user";//库名 $username="database";//用户 $pw="passwd";//密码 $dbh= new PDO("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo"Failed to get DB handle: ".$e->getMessage() ."n"; exit; } echo'connent MSSQL succeed'; $stmt=$dbh->prepare("select * from xmspace_net"); $stmt->execute(); while ($row=$stmt->fetch()) { print_r($row); } unset($dbh); unset($stmt); ?> |
测试是否能正常连接mssql2008