【问题标题】:Connecting to sql server data with PHP on ubuntu在 ubuntu 上使用 PHP 连接到 sql server 数据
【发布时间】:2017-01-18 03:03:31
【问题描述】:

尝试连接到我的 mssql 数据库时,我收到错误消息 “SQLSTATE[01002] Adaptive Server 连接失败(严重性 9)”

下面是我正在运行的php代码

<?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

【问题讨论】:

  • 在您的odbc.ini 中,您似乎有wTDS_Verison = 7.1 而不是TDS_Verison = 7.1

标签: php sql-server odbc freetds


【解决方案1】:

原来一切都配置正确,问题是使用 dblib 而不是 odbc,请参见下面的代码。

try {
    $hostname = "hostname.database.windows.net";
    $port = 1433;
    $dbName = "databasename";
    $dbuser = "dbuser@hostname";
    $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;
 }

【讨论】:

    【解决方案2】:

    2021 - 回到未来:

    我知道这篇文章已经过时了,但我的回复可以为绝望的灵魂指明正确的方向:

    1. 您正在使用 odbc 并且命令“isql”工作正常
    2. 您正在使用 FreeTDS,并且命令“tsql”工作正常
    3. 问题是运行通过 FreeTDS 调用 odbc 的 php 脚本 -> 不断出错

    请阅读: https://www.linuxquestions.org/questions/blog/tix-592494/freettds-libiodbc-iodbc-php-7-4-21-on-slackware-current-5-13-5-38621/

    欢呼快乐的灵魂!

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 2018-01-02
      • 1970-01-01
      • 2017-05-14
      • 2014-01-12
      • 2015-10-30
      • 2014-02-05
      • 2020-03-07
      相关资源
      最近更新 更多