使用 PDO_DBLIB 连接到 MS SQL 数据库的 PHP 错误 [英] PHP error connecting to MS SQL database using PDO_DBLIB

查看:16
本文介绍了使用 PDO_DBLIB 连接到 MS SQL 数据库的 PHP 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

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

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

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

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

我能够在不同的 Web 服务器上使用相同的代码连接到数据库,这让我相信这是一个配置问题.两台服务器上的 php.ini 文件看起来相对相同.它们都启用了相同的 PDO 库,并配置了相同的选项.

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.

有人知道为什么会发生这种情况吗?

Does anyone have any idea why this could be happening?

推荐答案

事实证明,这是一个比我想象的要简单得多的问题.无论出于何种原因,开发服务器没有使用端口 1433 作为连接中的默认端口,而是使用端口 4000.

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.

我通过启用 freetds.conf 文件中的日志并在我发出请求时监控它们发现了这一点.

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

另外,有一点需要注意:DBLIB 扩展使用冒号 (:) 作为主机和端口之间的分隔符,而不是逗号.不幸的是,您在使用逗号时收到的错误描述性不强,因此希望有人能从这一发现中受益.

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.

这篇关于使用 PDO_DBLIB 连接到 MS SQL 数据库的 PHP 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆