How to read Access database file
(mdb) using PHP

Microsoft Access support in PHP can be done via PECL’s ext/mdbtools extension, which is maintained by Hartmut Holzgraefe. This extension is itself a wrapper around the MDBTools package (https://github.com/brianb/mdbtools), which provides a set of tools to read Access database schema and records.

1. Install mdbtools
If you are running your server on Centos, it is easy to install mdbtools
yum install mdbtools
yum install mdbtools-devel

2. Install pecl mdbtools extension
login as root and run
pecl install mdbtools

If you like manual install, please download the latest from http://pecl.php.net/package/mdbtools, untar it, cd to the source folder then run the following:
phpize
./configure
make
make install

If you are running a cpanel server, you can install the pecl package via the WHM interface.

3. To use it
-. upload a mdb test file. You can get a sample mdb file from your Microsoft Access installation folder. We found Nwin.mdb

-. Here is a sample script to list all tables in the mdb and find out the number of records of each table:

// initialize database handle
$mdb = mdb_open('Nwind.mdb');
if ($mdb === false) {
die('ERROR: Cannot initialize database handle');
}

// get table listing
$tables = mdb_tables($mdb) or die('ERROR: Cannot get table listing');
if (sizeof($tables) > 0) {

echo '<h2>Table found: </h2>';

foreach ($tables as $t) {
echo 'opening table '. $t . '<br/>';

$tbl = mdb_table_open($mdb, $t) or die('ERROR: Cannot open table ' . $t);

// find and print number of records in table
$rows = mdb_num_rows($tbl);
echo "$t has ($rows) records <br/>\n";

// close table
mdb_table_close($tbl);
}
}

// destroy database handle
mdb_close($mdb);

This entry was posted in PHP Programming. Bookmark the permalink.