连接到mssql服务器时遇到问题

时间:2014-01-21 20:17:18

标签: php sql-server pdo

我用来测试连接的代码:

   $serverName = 'host\sqlexpress';
   $database = 'dbname';

   // Get UID and PWD from application-specific files.
   $uid = 'username';
   $pwd = 'password';

   try {
      $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
      $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
   }

   catch( PDOException $e ) {
      die( "Error connecting to SQL Server" );
   }

   echo "Connected to SQL Server\n";

   $query = 'select * from tablename';
   $stmt = $conn->query( $query );
   while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
      print_r( $row );
   }

   // Free statement and connection resources.
   $stmt = null;
   $conn = null;
?>

运行此文件时出现的错误只是“连接到SQL Server时出错”

任何线索为什么这可能不起作用?我能以任何方式获得更详细的错误报告吗?

0 个答案:

没有答案