使用PDO_DBLIB连接到MS SQL数据库。

时间:2022-09-26 08:42:59

I am attempting to use PHP's PDO_DBLIB driver to connect to a remote database and am having some issues.

我正在尝试使用PHP的PDO_DBLIB驱动程序连接到远程数据库,并遇到一些问题。

The database is connectable via the same environment using telnet and a SQL client. However, connecting using the following code in PHP does not work:

数据库通过使用telnet和SQL客户机的相同环境进行连接。但是,在PHP中使用以下代码进行连接不会起作用:

<?php
$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');

Running this code, whether it be from the command line or Apache, yields the following error:

运行此代码,无论是来自命令行还是Apache,都会产生以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'

致命错误:未捕获的异常'PDOException'与消息'SQLSTATE[HY000]无法连接:Adaptive Server不可用或不存在(严重9)'

I am able to connect to the database using the same code on a different web server, which leads me to believe that it's a configuration issue. The php.ini files on the two servers look relatively the same. They each have the same PDO libraries enabled with the same options configured.

我可以在不同的web服务器上使用相同的代码连接到数据库,这使我相信这是一个配置问题。php。两个服务器上的ini文件看起来相对相同。它们每一个都具有相同的PDO库,并配置了相同的选项。

Does anyone have any idea why this could be happening?

有人知道为什么会这样吗?

2 个解决方案

#1


16  

Turns out that it was a much simpler issue than I thought. For whatever reason, the development server was not using Port 1433 as the default port in the connection and was instead using Port 4000.

事实证明,这比我想象的要简单得多。不管出于什么原因,开发服务器没有使用端口1433作为连接的默认端口,而是使用端口4000。

I discovered this by enabling the logs in the freetds.conf file and monitoring them as I was making the request.

我通过在freetds中启用日志来发现这一点。在我提出请求时,conf文件并监视它们。

Also, something to note: The DBLIB extension uses a colon (:) as a separator between the host and the port instead of the comma. Unfortunately, the error that you receive when you use a comma isn't very descriptive, so hopefully someone benefits from this discovery.

还有一点要注意:DBLIB扩展使用冒号(:)作为主机和端口之间的分隔符,而不是逗号。不幸的是,当您使用逗号时所收到的错误并不是很有描述性,所以希望有人从这个发现中获益。

#2


3  

Write port into freetds.conf directly for this host:

端口写入freetds。conf直接为这个主机:

[RemoteServer]
    host = RemoteServer
    port = 1433

And leave php-code us is:

我们的php-code是:

$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');

#1


16  

Turns out that it was a much simpler issue than I thought. For whatever reason, the development server was not using Port 1433 as the default port in the connection and was instead using Port 4000.

事实证明,这比我想象的要简单得多。不管出于什么原因,开发服务器没有使用端口1433作为连接的默认端口,而是使用端口4000。

I discovered this by enabling the logs in the freetds.conf file and monitoring them as I was making the request.

我通过在freetds中启用日志来发现这一点。在我提出请求时,conf文件并监视它们。

Also, something to note: The DBLIB extension uses a colon (:) as a separator between the host and the port instead of the comma. Unfortunately, the error that you receive when you use a comma isn't very descriptive, so hopefully someone benefits from this discovery.

还有一点要注意:DBLIB扩展使用冒号(:)作为主机和端口之间的分隔符,而不是逗号。不幸的是,当您使用逗号时所收到的错误并不是很有描述性,所以希望有人从这个发现中获益。

#2


3  

Write port into freetds.conf directly for this host:

端口写入freetds。conf直接为这个主机:

[RemoteServer]
    host = RemoteServer
    port = 1433

And leave php-code us is:

我们的php-code是:

$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');