sqlsrv_connect无法连接到localhost

时间:2017-08-28 19:58:47

标签: php sql-server sql-server-2008 sqlsrv

我正在尝试使用MSSQL连接到sqlsrv_connect(),但它无效。我收到一个奇怪的错误:

/var/www/html/dir/mssql_conn.php: line 1: ?php: No such file or directory
/var/www/html/dir/mssql_conn.php: line 5: syntax error near unexpected token `('
/var/www/html/dir/mssql_conn.php: line 5: `$conn_array = array ('

我的代码是:

$conn_array = array (
    "UID" => "",
    "PWD" => "",
    "Database" => "dbName",
);


    $conn = sqlsrv_connect('(local)', $conn_array);


    if ($conn) {
        echo "connected";

        if(($result = sqlsrv_query($conn,"SELECT TOP 100 * FROM dbo.tblp")) !== false){

            while( $obj = sqlsrv_fetch_object( $result )) {
                  echo $obj->colName.'<br />';
            }
        }


    } else {
        die(print_r(sqlsrv_errors(), true));
    }

我也尝试了localhost\sqlexpress, 8357serv.domain.tld\sqlexpress, 8357,但没有任何作用。

从外面形成一个港口。但我不认为这会适用于内部吗?

我可以实际连接的任何帮助吗?

2 个答案:

答案 0 :(得分:0)

我认为这是因为您在$sumSpots = "SELECT SUM(Spots) FROM Bookings INNER JOIN Trips ON Bookings.FK_ID = Trips.ID"; $bookedSpots = mysql_query($sumSpots, MYSQL_ASSOC); 之后添加了一个逗号, 也许它应该是

"dbName"

答案 1 :(得分:0)

您可以通过以下代码检查连接

$serverName = "(local)\sqlexpress";  
$conn_array = array("UID" => "",
"PWD" => "",
"Database" => "dbName"
); // Provide the valid details 

/* Connect using Windows Authentication. */  
$conn = sqlsrv_connect( $serverName, $conn_array);  
if( $conn === false )  
die( FormatErrors( sqlsrv_errors() ) );

function FormatErrors( $errors )  
{  
    /* Display errors. */  
    echo "Error information: <br/>";  

    foreach ( $errors as $error )  
    {  
        echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";  
        echo "Code: ".$error['code']."<br/>";  
        echo "Message: ".$error['message']."<br/>";  
    }  
}

了解更多信息https://docs.microsoft.com/en-us/sql/connect/php/example-application-sqlsrv-driver