問題描述
我正在嘗試使用 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 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!