一般情况下都是php+mysql,asp.net+sqlserver这种使用。有客户反馈需要在他的centos系统下进行php连接sqlserver数据库,请求协助处理。
这里蓝队网络就把整体的安装设置流程记录分享下,希望能帮到也有同样需求的人。
登陆我们的服务器,我这里使用的是centos6.7 nginx+apache+php 我的php为5.4版本。
#cd /opt && wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
#tar zxvf freetds-patched.tar.gz
#cd freetds-0.95.87 && ./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib
# make && make install
安装完成后安装php扩展。
#cd /opt/php-5.4.45/ext/mssql
#/usr/local/php5.4/bin/phpize
#./configure --with-php-config=/usr/local/php5.4/bin/php-config --with-pdo-dblib=/usr/local/freetds/
#make && make install
编译完php的mssql扩展后编辑你的php配置文件开启扩展然后重启apache或者php-fpm(fastcgi)进程。
测试文件:
error_reporting(E_ERROR & ~E_NOTICE);
ini_set("display_errors",1);
ini_set("error_reporting",E_ALL);
ini_set("log_errors",1);
header("Content-type: text/html; charset=utf-8");
$msdb=mssql_connect("数据库主机地址:1433","用户","密码");
if (!$msdb) {
echo "connect sqlserver error";
exit;
}
mssql_select_db("数据库",$msdb);
$result = mssql_query("select @@version", $msdb);
while($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_free_result($result);
?>
运行截图:
可以看到成功连接上并执行了版本查询语句。