我如何从PHP连接到MSSQL数据库

时间:2016-09-29 16:40:07

标签: php sql sql-server iis

我有一个Windows Azure服务器,我想从PHP应用程序连接到MSSQL服务器。但是,当我尝试连接到数据库时,会弹出一条错误消息,说明......

Array ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) [1] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\IUSR'. ) ) 

我使用此代码连接数据库..

<?php
    $serverName = "AZR-SRV-MAP01"; //serverName\instanceName

    // Since UID and PWD are not specified in the $connectionInfo array,
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"EmpSys");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    if( $conn ) {
        echo "Connection established.<br />";
    }else{
        echo "Connection could not be established.<br />";
        die( print_r( sqlsrv_errors(), true));
    }
?>

我使用Windows身份验证访问数据库,因此我没有指定UIDPWD参数。

请帮我连接到SQL服务器。

2 个答案:

答案 0 :(得分:0)

您可以查看此已发布的有关此主题的答案。这似乎可以帮助你。

MSSQL connection using PHP

祝你好运:)

答案 1 :(得分:0)

从PHP连接到MSSQL数据库的工作代码(感谢gofr1)..

<?php
    $serverName = "server_name"; //serverName\instanceName

    // Since UID and PWD are not specified in the $connectionInfo array,
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"db_name");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    if( $conn ) {
        echo "Connection established.<br />";
    }else{
        echo "Connection could not be established.<br />";
        die( print_r( sqlsrv_errors(), true));
    }
?>

修改

您需要打开Windows身份验证,并在IIS设置中设置匿名身份验证。通过本地/域帐户授权SQL Server。