SaidbakR
SaidbakR

Reputation: 13544

PHP pdo ODBC and mdb file driver

I have PHP 5.5 on Windows 7 . I'm trying to access an MS Access data base file named main.mdb using PHP. I'm using the following code:

<?php
$dbName = "C:\\test\\main.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
// The following is line 7
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};charset=UTF-8; DBQ=$dbName; Uid=; Pwd=;");
//$db->exec("set names windows-1256");
$sql = "SELECT 0bok.*, `0cat`.name FROM 0bok INNER JOIN 0cat ON `0cat`.id = `0bok`.cat WHERE cat = 35 OR cat = 38 OR cat = 39 OR cat = 41 OR cat = 47 OR cat = 48 OR cat = 4 OR cat = 5 ORDER BY cat";
$result = $db->query($sql);
$row = $result->fetchAll();
$i = 1;

echo "<pre>";
foreach ($row as $book) {
    echo $i . ": " . iconv("windows-1256", "utf-8", $book['bk']) . ", " . $book['bkid'] . " -> " . iconv("windows-1256", "utf-8", $book['name'])  . "\n";
    $i++;
}

However, I have got the following error message:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\some\path\di.php:7 Stack trace: #0 C:\some\path\di.php(7): PDO->__construct('odbc:DRIVER={Mi...') #1 {main} thrown in C:\some\path\di.php on line 7

The question is: How could I got the missed driver and make it works with the code that I have regarded above?

Upvotes: 1

Views: 3986

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123839

PDO supports a number of different drivers but not all of them are enabled by default. For PDO_ODBC on Windows we need to ensure that the line

extension=php_pdo_odbc.dll

is enabled (not commented out) in php.ini.

Upvotes: 2

Related Questions