pdo连接sql server数据库

pdo连接sql server数据库

 

<?php
header("Content-type: text/html; charset=utf-8");
if(PATH_SEPARATOR==':')
    echo '本机操作系统是:Linux<br>';
else
    echo '本机操作系统是:Windows<br>';

try {
	//服务器
	$host = "127.0.0.1";
	$port = '1433';
	$dbname = "oblog46";
	$username = "sa";
	$pw = "123456";
	$dbh = null;

	if(PATH_SEPARATOR==':'){
		$dbh = new PDO ("dblib:host=$host:$port;dbname=$dbname","$username","$pw");
	}else{
		$dbh = new PDO("sqlsrv:Server=$host,$port;Database=$dbname",$username,$pw);
	}
} catch (PDOException $e) {
	echo "Failed to get DB handle: " . $e->getMessage() . "\n";
	exit;
}

$stmt = $dbh->prepare("SELECT top 3 * FROM oblog_log");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
	echo "<pre>";
	print_r($row);
}
unset($dbh); unset($stmt);

 

posted @ 2024-03-13 12:49  燕鹏  阅读(25)  评论(0)    收藏  举报  来源