使用php pdo 连接到mssql sqlsrv

先到官方http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 下载相应的驱动。 然后windows为例

extension=php_pdo_odbc.dll
extension=php_pdo_sqlsrv_54_nts.dll
extension=php_sqlsrv_54_nts.dll

查看phpinfo中是否已经开启MsSQL 接下来用代码测试即可

$DSN  = "mssql:host=localhost;dbname=testdb";//mssql dsn
$DSN="sybase:host=localhost;dbname=testdb";//sybase dsn
$DSN="dblib:host=localhost;dbname=testdb";//any dsn
$DSN = "sqlsrv:Server=localhost,1521;Database=testdb";//ms sql server
$dbUser = "dbuser";//username
$dbPass = "password";//password
$conn = new PDO($DSN,$dbUser,$dbPass);//connect handle
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);// set some attribute of pdo object
$query = "select * from table where field=:field;"; //T_SQL query 
$fieldValue = 'Value';//
$params = array(":field"=>$fieldValue);
$getProducts = $conn->prepare($query);
$getProducts->execute($params);
$products= $getProducts->fetchAll(PDO::FETCH_ASSOC);
updatedupdated2021-03-182021-03-18