1

I am trying to access a MDB file using PHP on a CentOS machine, preferably using PDO. I have installed the ODBC and pdo_odbc modules in PHP and restarted the the web server (Apache). I also installed mdbtools and unixODBC.

print_r(PDO::getAvailableDrivers());

Shows:

Array
(
    [0] => mysql
    [1] => odbc
    [2] => pgsql
    [3] => sqlite
)

I try to access the DBA file (which is saved locally) with this code:

try
{
    $driver = "/usr/lib64/libmdbodbc.so.0";
    $dbName = "/PATH/FILE.MDB";
    if (!file_exists($dbName)) {
        die("Could not find database file.");
    }
    $db = new PDO("odbc:DRIVER=$driver; DBQ=$dbName; Uid=; Pwd=;");

    $sql  = "SHOW TABLES";

    $result = $db->query($sql);
    $row = $result->fetch();

    var_dump($row);

}
catch(Exception $e)
{
    echo "\n\nEXCEPTION: $e\n\n";
}

And this outputs:

EXCEPTION: exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /PATH/odbctest.php:14
Stack trace:
#0 /PATH/odbctest.php(14): PDO->__construct('odbc:DRIVER=/us...') 
#1 {main}

What do I need to change? What should I use as a driver name? I have been looking all over the place and all I find is a lot of really old stuff from the 2004 era, but a lot of the links have disappeared over the years. This is the most relevant StackOverflow question, but does not include any code: PDO Microsoft Access. I really only need to read from the file.

EDIT:

With some tinkering it seems that I now at least have PDO happy with the driver by correcting the odbcinst.ini file which now contains the following:

[MDBTools]
Description = MDBTools Driver
Driver64    = /usr/lib64/libmdbodbc.so.0
Setup       = /usr/lib64/libmdbodbc.so.0
FileUsage   = 1
UsageCount  = 1

Unfortunately, I am still getting an error:

exception 'PDOException' with message 'SQLSTATE[08001] SQLDriverConnect: 1 [unixODBC]Could not find DSN in connect string' in /PATH/odbctest.php:15
Stack trace:
#0 /PATH/odbctest.php(15): PDO->__construct('odbc:Driver=MDB...')
#1 {main}

I am making the PDO call as follows:

$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver; DBQ=$dbName; Uid=; Pwd=;");

I tried adding the data source to the odbc.ini file even though I can't do this in the production version and am still getting the same error. I did find this which is interesting: DSN-less connection with PHP ODBC using MDBTools Driver. Any suggestions?

Community
  • 1
  • 1
user2395126
  • 526
  • 1
  • 7
  • 20
  • Maybe this is useful: https://gist.github.com/amirkdv/9672857 – bovino Marcelo Bezerra Feb 18 '15 at 21:13
  • Very useful! After adding the correct entry to the odbcinst.ini file it seems that PHP and PDO are now finding the correct driver, but now I am getting an error "Could not find DNS in connect string." I did not add an entry to odbc.ini as all the relevant info should be in the PDO call. Any ideas? – user2395126 Feb 18 '15 at 21:34

1 Answers1

0
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver;DBQ=$dbName", "", "");
Fábio Galera
  • 125
  • 11