在localhost上连接到SQL Server的PHP返回错误定位服务器/实例指定

时间:2011-08-30 20:28:06

标签: php sql sql-server

编辑:对不起,我弄清楚问题是什么。我正在编辑错误的文件。

我(尝试)使用PHP 5.3.5连接到我的SQL SERVER 2005,但它只是麻烦。在本地计算机上,如果我使用SQLCMD -S (local) -U sqlUser -P 1234 -d MyTable,则连接正常。但是以下代码会产生错误:

<?php
$myServer = "(local)";
$myUser = "sqlUser";
$myPass = "1234";
$myDB = "MyTable"; 
$connectionInfo = array("Database"=> $myDB, "UID" => $myUser, "PWD" => $myPass);


//connection to the database
try{
$dbhandle = sqlsrv_connect($myServer, $connectionInfo)
  or die(print_r( sqlsrv_errors(), true)); 
}catch(Exception $e){
     echo "<pre>";
     print_r($e);
     echo "</pre>";
     die;
}
?> 

如何让php连接到我的SQL Server?

完全错误:


Array
(
    [0] => Array
        (
            [0] => 08001
            [SQLSTATE] => 08001
            [1] => -1
            [code] => -1
            [2] => [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. 
            [message] => [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. 
        )

    [1] => Array
        (
            [0] => HYT00
            [SQLSTATE] => HYT00
            [1] => 0
            [code] => 0
            [2] => [Microsoft][SQL Server Native Client 10.0]Login timeout expired
            [message] => [Microsoft][SQL Server Native Client 10.0]Login timeout expired
        )

    [2] => Array
        (
            [0] => 08001
            [SQLSTATE] => 08001
            [1] => -1
            [code] => -1
            [2] => [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
            [message] => [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
        )

)

2 个答案:

答案 0 :(得分:0)

如果PHP不使用与SQLCMD相同的连接方法,您将会看到奇怪的结果。 PHP不仅仅使用TCPIP进行连接(意味着您的服务器也应该设置为侦听TCPIP)。

答案 1 :(得分:0)

愚蠢的错误,我正在编辑错误的文件。