linux下php如何連接sqlserver數(shù)據(jù)庫
2016-02-24 10:59:43
14016
一般情況下都是php+mysql,asp.net+sqlserver這種使用。有客戶反饋需要在他的centos系統(tǒng)下進(jìn)行php連接sqlserver數(shù)據(jù)庫,請求協(xié)助處理。
這里藍(lán)隊(duì)網(wǎng)絡(luò)就把整體的安裝設(shè)置流程記錄分享下,希望能幫到也有同樣需求的人。
linux下安裝php的mssql擴(kuò)展,首先需要安裝freetds.下載地址:ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
登陸我們的服務(wù)器,我這里使用的是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擴(kuò)展。
#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擴(kuò)展后編輯你的php配置文件開啟擴(kuò)展然后重啟apache或者php-fpm(fastcgi)進(jìn)程。
測試文件:
<?php
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("數(shù)據(jù)庫主機(jī)地址:1433","用戶","密碼");
if (!$msdb) {
echo "connect sqlserver error";
exit;
}
mssql_select_db("數(shù)據(jù)庫",$msdb);
$result = mssql_query("select @@version", $msdb);
while($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_free_result($result);
?>
運(yùn)行截圖:
可以看到成功連接上并執(zhí)行了版本查詢語句。