phpinfo显示已经成功了啊
报错:Error:SQLSTATE[IMSSP]: An invalid keyword 'host' was specified in the DSN string
求大神解答,已经困扰两天了,很紧急,在线等
测试代码如下:
$username = "sa";
$pwd = "246521";
try 
{  
   
   $dbh = new PDO('sqlsrv:host=localhost;dbname=test',$username,$pwd);   $sqlstri = "select * from test1";   foreach ($dbh->query($sqlstri) as $row)   {
print_r($row);   }
}
catch (PDOException $e)
{
   print "Error:".$e->getMessage()."<br>"; 
}

解决方案 »

  1.   

    无效的关键字'主机'是在DSN中指定的字符串手册的范例已经给出写法
    http://www.php.net/manual/zh/ref.pdo-sqlsrv.connection.phpExample #1 PDO_SQLSRV DSN examplesThe following example shows how to connecto to a specified MS SQL Server database:$c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
    The following example shows how to connect to a MS SQL Server database on a specified port:$c = new PDO("sqlsrv:Server=localhost,1521;Database=testdb", "UserName", "Password");
    The following example shows how to connecto to a SQL Azure database with server ID 12345abcde. Note that when you connect to SQL Azure with PDO, your username will be UserName@12345abcde (UserName@ServerId).$c = new PDO("sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName@12345abcde", "Password");
      

  2.   

    $c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
      

  3.   

    $dbh = new PDO("sqlsrv:Server=localhost;Database=test", $username , $password);