【详细错误】:Uncaught exception 'PDOException' with message 'SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]命名管道提供程序: 无法打开与 SQL Server 的连接 [2].
【解决办法】:
1. 从微软官方下载SQL Server 2012对PHP支持的应用程序:Microsoft Drivers 3.0 for PHP for SQL Server.
2. 释放文件后选择相应的DLL文件复制到PHP的Ext文件夹, 并在php.ini文件中加入相应的引用, 重启服务管理器.
3. 修改pdo的连接字符串:
$conn = new PDO("sqlsrv:Server=ip地址; Database=数据库名称", "用户名", "密码");
如果第二步不清楚应该如何选择DLL文件,请参照http://blog.csdn.net/abandonship/article/details/6461881,于此相类似~
okey,来测试一下:
$conn = new PDO("sqlsrv:Server=192.168.100.5; Database=TestDB", "BanL", "BanL123456"); $query = 'select top 1 * from _T'; $stmt = $conn->query( $query ); while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { print_r( $row ); } $conn = null;
/*Result:
Array ( [id] => 1 [value] => 11111 [createTime] => 2012-09-13 17:07:30.513 )
*/
Oh-Year, That's okey~