首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP连接到SQL Server 2014?

PHP连接到SQL Server 2014?
EN

Stack Overflow用户
提问于 2018-08-24 00:13:56
回答 1查看 0关注 0票数 0

我已经尝试通过使用PHP连接SQL Server以进行PDO和简单连接。但有些人总是会出现错误。这是我的脚本使用PDO和简单的连接。

使用PDO:

代码语言:javascript
复制
$serverName = "Server"; //serverName\instanceName
$connectionInfo = array( "Database"=>"Database", "UID"=>"Username", "PWD"=>"Password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
    echo "Connection established.<br />";
    $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!");
    fwrite($resultfile, "wew");
    fclose($resultfile); 
}else{
    echo "Connection could not be established.<br />";
    $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!");
    fwrite($resultfile , print_r( sqlsrv_errors(), true));
    fclose($resultfile ); 
    die( print_r( sqlsrv_errors(), true));
}

使用简单连接:

代码语言:javascript
复制
try  
    {  
        $conn = new PDO( "sqlsrv:server = $serverName; Database = Database", "password", "username");  
        $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
        echo "Connection established.<br />";
        $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!");
        fwrite($resultfile, "wew");
        fclose($resultfile); 
    }  
    catch(Exception $e)  
    {   $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!");
        fwrite($resultfile, "wew");
        fclose($resultfile);  
        die( print_r( $e->getMessage() ) );   
    }

以下是PDO的错误结果:

代码语言:javascript
复制
Array(
[0] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 21
        [code] => 21
        [2] => [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client.
        [message] => [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client.
    )

[1] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 87
        [code] => 87
        [2] => [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The parameter is incorrect.

        [message] => [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The parameter is incorrect.

    )

[2] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 21
        [code] => 21
        [2] => [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection
        [message] => [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection
    )

[3] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 87
        [code] => 87
        [2] => [Microsoft][ODBC Driver 13 for SQL Server]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][ODBC Driver 13 for SQL Server]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.
    ))

简单连接:

代码语言:javascript
复制
SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client.

我仍然不明白为什么我无法连接因为我使用最新的SQL Server驱动程序PHP 7.2.5。

EN

回答 1

Stack Overflow用户

发布于 2018-08-24 09:32:21

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档