PDO返回错误“找不到驱动器".与已知的工作DSN [英] PDO returning error "could not find driver" with a known working DSN

查看:109
本文介绍了PDO返回错误“找不到驱动器".与已知的工作DSN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过php的PDO类连接到odbc数据库:

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 :

致命错误:C:\ inetpub \ wwwroot \ pdoClass.php:7中消息未找到驱动程序的未捕获异常'PDOException'堆栈跟踪:#0 C:\ inetpub \ wwwroot \ pdoClass.php(7) :PDO-> __ construct('odbc:CS_HDZipCo ...','demo','skdemo!')#1 {main}放在第7行的C:\ inetpub \ wwwroot \ pdoClass.php中

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

$dsn值是我在ODBC管理器中创建的DSN的名称.

The $dsn value is the name of the DSN I created in my ODBC Manager.

我知道这个特定的DSN可以工作,因为我能够构建另一个演示文件并通过odbc_connect成功连接:

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));

我一直在研究 PDO-ODBC 文档以及其他在线资源,但是我无法弄清楚为什么从PDO尝试时PHP无法找到驱动程序.

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.

有什么想法吗?

我弹出打开phpinfo页面,以确保根据Marc B的评论安装了odbc驱动程序:

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.

根据Marc B的附加评论对我的phpini进行进一步检查时,似乎我没有安装POD ODBC特定的驱动程序:

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:

所以在这里,如果我安装了用于pdo的ODBC驱动程序,odbc将在列表的末尾,对吗?

So here, if I had the ODBC driver for pdo installed, odbc would be at the end of the list, correct?

推荐答案

在最初的问题评论中得到Marc B的大力帮助后,事实证明问题出在我对在Web服务器上启用odbc的误解和安装了pdo_odbc驱动程序.

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.

虽然我确实在Web服务器上启用了odbc,但我没有为PDO安装odbc驱动程序.

While I did have odbc enabled on the web server, I did not have the odbc driver installed for PDO.

此驱动程序对于能够通过pdo访问odbc数据库是必需的.

This driver is necessary to be able to access odbc databases via pdo.

根据为Windows安装pdo 上的php.net文档,该驱动程序已包含在php build中(我使用的是5.5版),只需要包含在php.ini文件中即可.

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:

,并且在演示数据库上使用PDO进行的测试查询有效:

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);
}

转储:

感谢您对Marc B的帮助.

Thanks for your help Marc B.

这篇关于PDO返回错误“找不到驱动器".与已知的工作DSN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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