首先需要安装Microsoft Drivers 3.0 for PHP for SQL Server,链接:http://www.microsoft.com/en-us/download/details.aspx?id=20098
根据需要将对应的dll加入到php.ini的extension中,我用的是php_pdo_sqlsrv_54_ts.dll
如此PDO的环境配置完成
测试一下:
<?php
$username = $_GET["username"];
$password = $_GET["password"];
$conn = new PDO("sqlsrv:Server=localhost;Database=master", "sa", "sa");
$sql = "SELECT * FROM users WHERE username = '$username' and password = '$password'";
$result = $conn->query($sql);
if ($result->fetch())
echo 1;
else
echo 0;
?>
数据库如图所示:
根据下面的结果可以看到成功连上了数据库