Connect to MS Access remote .mdb file from php on linux

随声附和 提交于 2019-12-03 16:36:15

Finally, I found solution.

  1. Set up on Win server FreeSSHd, configure connection account and set directory to one, you need
  2. Set up on unix server sshfs
  3. Mount Win server directory with .mdb files

    sshfs {user}@:/ {unix mount point} -o workaround=rename,allow_other

  4. Set up on unix server mdbtools

So, I used default PHP code from docs and write this PHP script:

$rows = $cols = array();
if (($handle = popen('/usr/bin/mdb-export {unix mount point}/{file}.mdb {table} 2>&1', 'r')) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        $num = count($data);
        if ($row == 1) { for ($c=0; $c < $num; $c++) { $cols[] = $data[$c]; } }
        else { for ($c=0; $c < $num; $c++) { $rows[$row][$cols[$c]] = $data[$c]; } }
        $row++;
    }
    pclose($handle);
}
print_r($rows);
  • Path to /usr/bin/mdb-export should be path to your mdb-export file (use find / -name "mdb-export", if you can't find yours).
  • Mount point {unix mount point} should be an empty file folder (I used /usr/home/remotemdb)
  • Table {table} should be the table name inside mdb file. Query all possible tables inside mdb file with command mdb-tables {unix mount point}/<file>.mdb

There is no need for drivers, configuration or other stuff, just plain mdbtools and access to file, in this case, achieved with remote connection through ssh. In you want, you can install fuse package, to autmatically mount remote directory, but that is another question.

Hope someone this helps.

You don't connect to a "server dsn". DSN's are a local thing only. They're not exposed for remote connections at all. If you want a machine to connect to a database, you need to have a DSN configured on that machine - you won't be able to use a DSN specified elsewhere.

For PHP ODBC, that'd be

$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=/network/path/to/your/access/database.mdb", $user, $password);

You are correct insomuch that you require an ODBC to ODBC Bridge.

At OpenLInk we refer to a Multi-tier ODBC to ODBC Bridge...

This is Multi-tier in the sense that it has a client/server architecture as follows --

Linux Client -- ODBC Application OpenLink Generic ODBC Driver

Windows Server -- 32bit OpenLink request Broker 32bit OpenLink ODBC Agent 32bit Microsoft Access ODBC Driver (with pre configured DSN) Microsoft Access Database file.

Nick Gorham

Its commercial, so possibly not of interest, but Easysoft have an ODBC driver for Access that's available on Most *nix's. No bridge required. There isn't a build on FreeBSD at the moment, but I could get one built for you on Monday if it's of any interest.

There is the open source MDB tools that may have enough for what you want, but it is lacking in quite a lot of functionality.

Easysoft Access ODBC Driver

MDB tools

Use PDO with MDBTools:

install:

apt-get install libodbc1

apt-get install libmdbodbc1

apt-get install php5-odbc

(restart apache)

Sample:

$query = 'SELECT * FROM Table';
$mdb_file = 'file.mdb';
$driver = 'MDBTools';
$dataSourceName = "odbc:Driver=$driver;DBQ=$mdb_file;Uid=user;Pwd=pass;";
$connection = new \PDO($dataSourceName);
$result = $connection->query($query)->fetchAll(\PDO::FETCH_ASSOC);
print_r($result);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!