[英]Apache will not connect to SQL Server with PHP
I'm trying to open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. 我试图通过apache linux服务器从Windows计算机上打开到SQL Server 2008的连接。 I've opened up the appropriate port on the firewall, but I am getting the vaguest error
我已经在防火墙上打开了适当的端口,但出现了最模糊的错误
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx
警告:mssql_connect()[function.mssql-connect]:无法连接到服务器:xxx.xxx.xx.xxx,xxxx
with: 有:
$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());
Is there some way to get a more specific error message? 有什么方法可以获取更具体的错误消息? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?
还是可以通过某种方式测试以查看两台计算机是否完全通信,以便尝试定位问题?
Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment... 这是我用来将PHP从Ubuntu机器连接到Windows SQL Server并将PHP连接到MSSQL的代码,我不知道它是否对您有帮助,但是此代码现在已经成功启动并运行,因此我知道它可以在我们的环境中工作。 。
As you can see, I use PDO rather than the mssql_* functions. 如您所见,我使用PDO而不是mssql_ *函数。 On Ubuntu I needed to install the php5-sybase package to get the dblib driver.
在Ubuntu上,我需要安装php5-sybase软件包以获取dblib驱动程序。
PHP: 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 /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 /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 /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
I ran into the same problem today. 我今天遇到了同样的问题。 This works for me:
这对我有用:
Instead of this 代替这个
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
try this: 尝试这个:
$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file
In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity 在您的情况下,为清楚起见,我将条目重命名并在配置文件中使用标准主机名作为条目
# Define a connection to the MSSQL server.
[mssqldbAlias]
host = mssqldb.mydomain.com
port = 1433
tds version = 8.0
The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file. mssql_connect()调用中的第一个参数必须与freetds.conf文件中的[entry]相对应。 So your call becomes
所以你的电话变成
$myServer = "mssqldbAlias";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.