<?php header("Content-type: text/html; charset=utf-8"); try { $hostname = "数据库IP"; $port = 1433; $dbname = "数据库名"; $username = "blog.csdn.net.unix21"; $pw = "password"; $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } $stmt = $dbh->prepare("SELECT top 5 * FROM tablename"); $stmt->execute(); while ($row = $stmt->fetch()) { print_r($row); } unset($dbh); unset($stmt); ?>
linux环境 宝塔 连接sql server 2008
SqlServer数据库端口默认是1433
1.首先需要编译安装FreeTDS
说明:一定要从官网下载最新的版本FreeTDS-# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
# tar -zxvf freetds-patched.tar.gz
# cd freetds-1.00.94
需要注意的就是这里的--with-tdsver=7.3,这个非常重要,你需要根据你的数据库版本选择正确的配置项,由于现在大多是SQLserve2008所以需要选择7.3.
# ./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib
# make && make install
配置FreeTDS
# cd ../
# echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
# ldconfig
验证FreeTDS版本
这一步非常重要,通过才可以继续,不然后面的步骤都是无意义的。
首先看看版本信息
# /usr/local/freetds/bin/tsql -C
测试数据库是否联通
# /usr/local/freetds/bin/tsql -H 数据库服务器IP -p 端口号(1433) -U 用户名 -P 密码
3.添加PHP扩展pdo的pdo_dblib
cd /root/
下载php-5.6.30.tar.gz
#tar -zxvf php-5.6.30.tar.gz
#cd /root/php-5.6.30/ext/pdo_odbc
#/www/server/php/56/bin/phpize
#./configure --with-php-config=/www/server/php/56/bin/php-config --with-pdo-#dblib=/usr/local/freetds/
#make && make install