PHP扩展 PDO_DBLIB 连接远程 MSSQL

PHP扩展 PDO_DBLIB 连接远程 MSSQL 数据库,需要一定的配置,这个以后再细说,直接看例子吧。

<?php
$dbhost		= '1.2.3.4';
$dbport		= 1433;
$dbuser		= 'yang';
$dbpass		= 'yangjunwei';
$dbname		= 'UserInfo';
$mssql		= new PDO('dblib:host='.$dbhost.':1433;dbname='.$dbname, $dbuser, $dbpass);

// 或使用如下直接连接 MSSQL
//$mssql = new PDO('dblib:host=1.2.3.4:1433;dbname=UserInfo','yang','yangjunwei');
?>

读取数据示例

<?php
$mssql = new PDO('dblib:host=1.2.3.4:1433;dbname=UserInfo','yang','yangjunwei');

if( !$mssql ){
	die("连接错误啦!".mssql_error()).'<br /><br />';
}else{
	echo "连接成功了!<br /><br />";
}

$sql = "select * from UInfo";
$res = $mssql->prepare($sql);
$res->execute();
while ($row = $res->fetch()) {
	echo "<pre>";
	print_r($row);
	echo "</pre>";
}
unset($mssql);
unset($res);
mssql_close($mssql);
?>