2

我正在尝试通过 php 从 apache linux 服务器打开与 Windows 计算机上的 SQL Server 2008 的连接。我已经在防火墙上打开了适当的端口,但我得到了最模糊的错误

警告:mssql_connect() [function.mssql-connect]:无法连接到服务器:xxx.xxx.xx.xxx,xxxx

和:

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die( mssql_get_last_message()); 

有没有办法获得更具体的错误信息?或者我可以通过某种方式测试两台计算机是否正在通信,以便我可以尝试开始定位问题?

4

2 回答 2

2

这是我用来将 PHP 连接到 MSSQL 从 Ubuntu 机器到 Windows SQL Server 的代码,我不知道它是否对你有帮助,但这段代码现在已经成功运行,所以我知道它可以在我们的环境中工作。 .

如您所见,我使用 PDO 而不是 mssql_* 函数。在 Ubuntu 上,我需要安装 php5-sybase 包来获取 dblib 驱动程序。

PHP:

<?php
try{
   $con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
   echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
   exit;
}
?>

/etc/odbc.ini

# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description             = MSSQL Server
Driver                  = freetds
Database                = MyDB
ServerName              = mssqldb
TDS_Version             = 8.0

/etc/odbcinst.ini

# Define where to find the driver for the Free TDS connections.
[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount      = 1

/etc/freetds/freetds.conf

[global]
        # 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

# Define a connection to the MSSQL server.
[mssqldb]
        host = mssqldb
        port = 1433
        tds version = 8.0
于 2013-01-24T21:10:03.723 回答
2

我今天遇到了同样的问题。这对我有用:

而不是这个

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon

尝试这个:

$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file

在您的情况下,为了清楚起见,我将重命名条目并将完全限定的主机名用于配置文件中的条目

# Define a connection to the MSSQL server.
[mssqldbAlias]
        host = mssqldb.mydomain.com
        port = 1433
        tds version = 8.0

mssql_connect() 调用中的第一个参数必须对应于 freetds.conf 文件中的 [entry]。 所以你的电话变成了

$myServer = "mssqldbAlias"; 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die( mssql_get_last_message());
于 2013-08-22T15:02:44.843 回答