本文介绍了使用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:

我能够在不同的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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:54