Connecting PHP on Linux to Microsoft Access on a Windows share

Easysoft's Access ODBC driver enables you to work with MDB or ACCDB format databases in environments where both Linux and Windows users need to access that database.

For example, you might want to publish a Windows-based Access database on an intranet web site based on a Linux server. This is the example scenario covered in this blog.

To do this:

  1. Download the Access ODBC driver and install and license it on your Linux web server machine.
  2. Modify the ACCESS_SAMPLE data source in odbc.ini so that it connects to the database on your Windows share. To do this:
    [ACCESS_SAMPLE]
    Driver=Easysoft ODBC-ACCESS
    # Path to the database on Linux machine
    mdbfile=/home/samba/ms_access/Northwind.accdb
    # The SMB URL for the database file. Use this syntax for the SMB URL:
    # smb://windows_host/share/path/filename
    smbpath=smb://windows_machine/accounts_share/ms_access/Northwind.accdb
    # The path to the libsmbclient library on the Access ODBC driver machine.
    smblib=/usr/lib/libsmbclient.so
    smbuser=my_windows_user
    smbauth=my_windows_password
    readonly=yes
    exclusive=no
    ignore_rel=no
  3. Test that you can connect in isql. For example:
    cd /usr/local/easysoft/unixODBC/bin
    ./isql.sh -v ACCESS_SAMPLE
  4. Install the PHP PDO-ODBC package and create a PHP page under your web server that connects to the Access database. For example:
    <?php
      $con = odbc_connect("odbc:ACCESS_SAMPLE", "", "");
      $err = odbc_errormsg();
      if (strlen($err) <> 0) {
        echo odbc_errormsg();
      } else {
        $rs2 = odbc_exec($con, "select * from Suppliers");
        odbc_result_all($rs2);
        odbc_close($con);
      }
    ?>

Note You need to set permissions so that the user the web server is running under, for example, www-data, has write access to the Access database file.