php PDO 使用已知的 DSN 返回错误“找不到驱动程序”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31813574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO returning error "could not find driver" with a known working DSN
提问by Chris Schmitz
I'm trying to connect to an odbc database via php's PDO
class:
我正在尝试通过 php 的PDO
类连接到 odbc 数据库:
$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:\inetpub\wwwroot\pdoClass.php:7 Stack trace: #0 C:\inetpub\wwwroot\pdoClass.php(7): PDO->__construct('odbc:CS_HDZipCo...', 'demo', 'skdemo!') #1 {main} thrown in C:\inetpub\wwwroot\pdoClass.php on line 7
致命错误:在 C:\inetpub\wwwroot\pdoClass.php:7 堆栈跟踪:#0 C:\inetpub\wwwroot\pdoClass.php(7): PDO- >__construct('odbc:CS_HDZipCo...', 'demo', 'skdemo!') #1 {main} 在第 7 行的 C:\inetpub\wwwroot\pdoClass.php 中抛出
The $dsn
value is the name of the DSN I created in my ODBC Manager.
该$dsn
值是我在 ODBC 管理器中创建的 DSN 的名称。
I know this particular DSN works because I was able to build another demo file and connect successfully via odbc_connect
:
我知道这个特定的 DSN 有效,因为我能够构建另一个演示文件并通过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-ODBCdocumentation as well as other resources online, but I can't figure out why PHP is unable to find the driver when trying from PDO.
我一直在挖掘PDO-ODBC文档以及其他在线资源,但我无法弄清楚为什么 PHP 在从 PDO 尝试时无法找到驱动程序。
Any ideas?
有任何想法吗?
Update
更新
I popped open my phpinfo page to make sure the odbc driver is installed per Marc B's comment:
我打开我的 phpinfo 页面以确保根据 Marc B 的评论安装了 odbc 驱动程序:
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:
根据 Marc B 的附加评论进一步检查我的 phpini,看起来我没有安装 POD ODBC 特定驱动程序:
So here, if I had the ODBC driver for pdo installed, odbc
would be at the end of the list, correct?
所以在这里,如果我安装了 pdo 的 ODBC 驱动程序,odbc
会在列表的末尾,对吗?
采纳答案by Chris Schmitz
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.
在最初问题的评论中从 Marc B 那里得到了一些很大的帮助后,结果证明问题出在我对odbc
在我的 Web 服务器上启用并pdo_odbc
安装驱动程序的误解。
While I did have odbc enabled on the web server, I did not have the odbc driver installed for PDO.
虽然我确实在 Web 服务器上启用了 odbc,但我没有为 PDO 安装 odbc 驱动程序。
This driver is necessary to be able to access odbc databases via pdo.
该驱动程序是通过 pdo 访问 odbc 数据库所必需的。
Per the php.net documentation on installing pdofor 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.
根据有关为 Windows安装 pdo的 php.net 文档,该驱动程序已包含在 php 版本中(我使用的是 5.5 版),只需将其包含在php.ini
文件中即可。
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:
我在演示数据库上使用 PDO 的测试查询有效:
$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.
感谢您的帮助马克 B。