2016-09-09 65 views
1

連接到SQL Server的數據與PHP當試圖連接到我的MSSQL數據庫,我得到的錯誤 「SQLSTATE [01002] Adaptive Server連接失敗(嚴重性9)」在Ubuntu

下面是PHP代碼我「M運行

<?php 

    try { 
    $hostname = "hostname.database.windows.net"; 
    $port = 1433; 
    $dbname = "database-dev"; 
    $username = "dbuser"; 
    $pw = "dbpassword"; 
    $dbh = new PDO  ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); 
    } catch (PDOException $e) { 
    echo "Failed to get DB handle: " . $e->getMessage() . "\n"; 
    exit; 
    } 
    $stmt = $dbh->prepare("select name from master..sysdatabases where  name = db_name()"); 
    $stmt->execute(); 
    while ($row = $stmt->fetch()) { 
    print_r($row); 
    } 
    unset($dbh); unset($stmt); 
?> 

而下面是我的ODBC.INI,ODBCINST.INI和freetds.conf,你可以看到我的phpinfo()這裏( 「http://wingedw.com/matiks/connect.php」)的驅動程序設置爲freetds的和PDO和pdo_dlib模塊已被添加到PHP 5,爲什麼即時通訊錯誤的任何線索,即時確認憑證是正確的。

ODBC.INI

[MSSQLServer] 
Driver = FreeTDS 
Description = Any description 
Trace = No 
Server = servername 
Port = 1433 
Database = dbname 
wTDS_Verison = 7.1 

ODBCINST.INI

[FreeTDS] 
Driver  = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so 
Setup   = /usr/lib/i386-linux-gnu/odbc/libtdsS.so 
UsageCount  = 1 

freetds.conf

[global] 
    # TDS protocol version 
;  tds version = 7.1 

    # Whether to write a TDSDUMP file for diagnostic purposes 
    # (setting this to /tmp is insecure on a multi-user system) 
;  dump file = /tmp/freetds.log 
;  debug flags = 0xffff 

    # Command and connection timeouts 
;  timeout = 100 
;  connect timeout = 100 

    # If you get out-of-memory errors, it may mean that your client 
    # is trying to allocate a huge buffer for a TEXT field. 
    # Try setting 'text size' to a more reasonable limit 
    text size = 64512 

# A typical Sybase server 
[egServer50] 
    host = symachine.domain.com 
    port = 5000 
    tds version = 7.1 

# A typical Microsoft server 
[MSSQLServer] 
    host = servername 
    port = 1433 
    tds version = 7.1 
+0

在你的'odbc.ini'中,你似乎有'wTDS_Verison = 7.1'而不是'TDS_Verison = 7.1'。 – FlipperPA

回答

0

原來一切都配置正確,這個問題是使用DBLIB在ODBC,請代碼如下。

try { 
    $hostname = "hostname.database.windows.net"; 
    $port = 1433; 
    $dbName = "databasename"; 
    $dbuser = "[email protected]"; 
    $dbpass = "password"; 
$dbh = new PDO('odbc:DRIVER=FreeTDS;SERVERNAME=mssql;DATABASE=' . $dbName, 
      $dbuser, $dbpass); 
    //echo "COnnected"; 

    } catch (PDOException $e) { 
    echo "Failed to get DB handle: " . $e->getMessage() . "\n"; 
    exit; 
}