Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO returning error "could not find driver" with a known working DSN

Tags:

php

pdo

odbc

dsn

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:\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

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

odbc manager's dsns

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:

odbc in phpini

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:

enter image description here

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

like image 350
Chris Schmitz Avatar asked Aug 04 '15 15:08

Chris Schmitz


1 Answers

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.

missing odbc driver

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.

including php_pdo.dll

After adding the driver and restarting the server I now had the driver loaded:

enter image description here

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:

pdo var dump

Thanks for your help Marc B.

like image 72
Chris Schmitz Avatar answered Oct 04 '22 18:10

Chris Schmitz