<\/script>')

PDO return error "could not find driver" with known working DSN

I am trying to connect to odbc database via php PDO class:

 $dsn = 'odbc:CS_HDZipCodes32bit'; $username = 'demo'; $password = 'skdemo!'; $connection = new PDO($dsn, $username, $password); die( var_dump( $connection ) ); 

but when I do this, I get an error:

Fatal error: throw a "PDOException" exception with the message "driver could not be found" 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} is thrown at C: \ inetpub \ wwwroot \ pdoClass.php on line 7

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

dsns odbc manager

I know this particular DSN works because I managed to create another demo file and successfully connect 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 searched the PDO-ODBC documentation, as well as other resources on the Internet, but I cannot understand why PHP could not find the driver when trying to execute PDO.

Any ideas?

Update

I opened the phpinfo page to make sure the odbc driver is installed on a Marc B comment:

odbc in phpini

It looks like the driver is installed if it is not another driver.

Another update

When further checking my additional comment on phpini for Marc B, it looks like I don't have the ODBC POD driver installed:

enter image description here

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

+7
php pdo odbc dsn
source share
1 answer

After receiving some great help from Marc B, the first comments of the question reveal that the problem arose because of my misunderstanding of enabling odbc on my web server and installing the pdo_odbc driver.

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

missing odbc driver

This driver is required to access odbc databases through pdo.

In the php.net documentation on installing pdo for windows, the driver was already included in the php assembly (I am using version 5.5) and just need to be included in the php.ini .

including php_pdo.dll

After adding the driver and rebooting the server, I now had the driver loaded:

enter image description here

and my test query using PDO in 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); } 

Resets:

pdo var dump

Thank you for your help Mark B.

+7
source share

All Articles