問題描述
我正在嘗試通過 php 的 PDO
類連接到 odbc 數據庫:
$dsn = 'odbc:CS_HDZipCodes32bit';$用戶名 = '演示';$password = 'skdemo!';$connection = new PDO($dsn, $username, $password);死( var_dump( $connection ) );
但是當我這樣做時,我收到錯誤:
<塊引用>致命錯誤:在 C:inetpubwwwrootpdoClass.php:7 堆棧跟蹤:#0 C:inetpubwwwrootpdoClass.php(7) 中出現消息找不到驅動程序"的未捕獲異常PDOException": PDO->__construct('odbc:CS_HDZipCo...', 'demo', 'skdemo!') #1 {main} 在第 7 行的 C:inetpubwwwrootpdoClass.php 中拋出
$dsn
值是我在 ODBC 管理器中創建的 DSN 的名稱.
我知道這個特定的 DSN 有效,因為我能夠構建另一個演示文件并通過 odbc_connect
成功連接:
$connection = odbc_connect("CS_HDZipCodes32bit", 'demo', 'skdemo!');如果(!$連接){die('連接失敗');}$statement = "SELECT * FROM ZipCodes";$result = odbc_exec($connection, $statement);//按預期輸出拉鏈var_dump(odbc_result_all($result));
我一直在研究
看起來驅動程序已安裝,除非這是不同的驅動程序.
另一個更新
根據 Marc B 的附加評論進一步檢查我的 phpini,看起來我沒有安裝 POD ODBC 特定驅動程序:
這里,如果我安裝了 pdo 的 ODBC 驅動程序,odbc
將位于列表的末尾,對嗎?
在最初問題的評論中得到 Marc B 的大力幫助后,結果證明問題出在我對啟用 odbc
的誤解在我的 Web 服務器上并安裝了 pdo_odbc
驅動程序.
雖然我確實在 Web 服務器上啟用了 odbc,但我沒有為 PDO 安裝 odbc 驅動程序.
此驅動程序是通過 pdo 訪問 odbc 數據庫所必需的.
根據關于
添加驅動程序并重新啟動服務器后,我現在加載了驅動程序:
并且我在演示數據庫上使用 PDO 的測試查詢有效:
$dsn = 'odbc:CS_HDZipCodes32bit';$用戶名 = '演示';$password = 'skdemo!';$connection = new PDO($dsn, $username, $password);$query = "從郵政編碼中選擇 *";$result = $connection->query($query);foreach($result 作為 $row){var_dump($row);}
轉儲:
感謝馬克 B 的幫助.
I'm trying to connect to an odbc database via php's PDO
class:
$dsn = 'odbc:CS_HDZipCodes32bit';
$username = 'demo';
$password = 'skdemo!';
$connection = new PDO($dsn, $username, $password);
die( var_dump( $connection ) );
but when I do, I get the error :
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:inetpubwwwrootpdoClass.php:7 Stack trace: #0 C:inetpubwwwrootpdoClass.php(7): PDO->__construct('odbc:CS_HDZipCo...', 'demo', 'skdemo!') #1 {main} thrown in C:inetpubwwwrootpdoClass.php on line 7
The $dsn
value is the name of the DSN I created in my ODBC Manager.
I know this particular DSN works because I was able to build another demo file and connect successfully via odbc_connect
:
$connection = odbc_connect("CS_HDZipCodes32bit", 'demo', 'skdemo!');
if(!$connection){
die('connection failed');
}
$statement = "SELECT * FROM ZipCodes";
$result = odbc_exec($connection, $statement);
// Outputs the zips as expected
var_dump(odbc_result_all($result));
I've been digging through the PDO-ODBC documentation as well as other resources online, but I can't figure out why PHP is unable to find the driver when trying from PDO.
Any ideas?
Update
I popped open my phpinfo page to make sure the odbc driver is installed per Marc B's comment:
It looks like the driver is installed unless this is a different driver.
Another Update
On further inspection of my phpini per Marc B's additional comment, it looks like I don't have the POD ODBC specific driver installed:
So here, if I had the ODBC driver for pdo installed, odbc
would be at the end of the list, correct?
After getting some great help from Marc B in the initial question's comments it turns out the problem was coming from my misunderstanding of enabling odbc
on my web server and having the pdo_odbc
driver installed.
While I did have odbc enabled on the web server, I did not have the odbc driver installed for PDO.
This driver is necessary to be able to access odbc databases via pdo.
Per the php.net documentation on installing pdo for windows, the driver was already included in the php build (I'm using version 5.5) and just needed to be included in the php.ini
file.
After adding the driver and restarting the server I now had the driver loaded:
and my test query using PDO on my demo database worked:
$dsn = 'odbc:CS_HDZipCodes32bit';
$username = 'demo';
$password = 'skdemo!';
$connection = new PDO($dsn, $username, $password);
$query = "Select * FROM ZipCodes";
$result = $connection->query($query);
foreach($result as $row){
var_dump($row);
}
Dumps out:
Thanks for your help Marc B.
這篇關于PDO 返回錯誤“找不到驅動程序"使用已知的 DSN的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!