linux連sql server 2012 和開啟PHP sqlserver擴展
阿新 • • 發佈:2018-03-21
linux sqlserver php連接sql server 2012數據庫
http://www.freetds.org/userguide/choosingtdsprotocol.htm
下載安裝 ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.20.tar.gz
./configure --prefix=/usr/local/freetds --with-tdsver=7.4 --enable-msdblib
make && make install
驗證版本 /usr/local/freetds/bin/tsql -C
連接sql server 2012數據庫
/usr/local/freetds/bin/tsql -H 192.168.1.200 -p 1433 -U username -P password
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/mssql.so
http://www.freetds.org/userguide/choosingtdsprotocol.htm
下載安裝 ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.20.tar.gz
./configure --prefix=/usr/local/freetds --with-tdsver=7.4 --enable-msdblib
make && make install
驗證版本 /usr/local/freetds/bin/tsql -C
連接sql server 2012數據庫
/usr/local/freetds/bin/tsql -H 192.168.1.200 -p 1433 -U username -P password
安裝拓展
mssql
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds
make && make install
pdo_dblib
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds
make && make install
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/pdo_dblib.so
重啟後測試testsqldb.php
mssql方式
<?php header("Content-type: text/html; charset=utf-8"); //$msdb=mssql_connect("數據庫IP","用戶名","密碼"); $msdb=mssql_connect("192.168.1.200:1433","username","password"); if (!$msdb) { echo "connect sqlserver error"; exit; } mssql_select_db("SCCMdata",$msdb); $result = mssql_query("select * from Agents", $msdb); while($row = mssql_fetch_array($result)) { print_r($row); } mssql_free_result($result); ?>
pdo_dblib方式
<?php
header("Content-type: text/html; charset=utf-8");
try {
$hostname = "192.168.1.200";
$port = 1433;
$dbname = "SCCMdata";
$username = "username";
$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 * from Agents");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
linux連sql server 2012 和開啟PHP sqlserver擴展