install php odbc extension

April 7, 2016 686 views
PHP Ubuntu

I'm trying to connect to a MS Access db via PHP using ODBC. I've tried with the PHP PDO lib as well as ADODB ( -- but neither works.

PDO returns: could not find driver: PDO->_construct('odbc:DRIVER={Mi...')
ADODB returns: Call to undefined function odbc

I think I've installed the ODBC extension properly. I installed:

apt-get install libmdbodbc1
apt-get install libodbc1
apt-get install php5-odbc
apt-get install libmyodbc

...basically every odbc related lib I came across. still no luck.

Any tips or suggestions?

3 Answers

I came here looking for a resolution to an issue reading an access database. This GIST article may be of use.

I can access the database using mdbtools but from apache a fault is thrown the first time I try to issue a prepared statement.

Did you get this working?

  • No, I didn't get it working. I ended up giving up and devising other methods for accomplishing what I needed.

    In reading various things online, I came to the conclusion that's pretty hard to get and ODBC connection to an .mdb database working in a linux environment. If running apache/mysql/php on windows, it's fairly trivial to install the libs and make the connections. But linux doesn't play well with .mdb (not much of a surprise there). That said -- if you sort it out I would love to know how.

    • I ended up copying the database into mysql. I could do this as it was only a few thousand rows in 4 or 5 tables. I do have to do this though every time someone runs the report incase any of the data is changed.

      So, its all scripted based on a number of minutes since last import. If not enough time has elapsed then use the existing data else requery access and move it to mysql.

      Might help someone (or future me);

      //get the database schema
      $command = "mdb-schema ". path('storage') . "database/{$mdb} mysql 2>&1";
      //prepend mysql commands 
      $schema = "DROP DATABASE IF EXISTS {$db};\nCREATE DATABASE {$db};\nUSE {$db};\n" . $schema;
      //create the table structure
      file_put_contents(path('storage') . "database/mysql-import", $schema);
      $command = "mysql -udbuser -premoved < " . path('storage') . "database/mysql-import 2>&1";
      //get the actual data by iterating over the tables      
      $command = "mdb-tables ". path('storage') . "database/{$mdb} -d^ 2>&1";
      foreach(explode("^",$tables) as $table){
          if(substr($table,0,1)<>'~' && strlen($table)>3){

      and then each table

      //copy a table from given mdb file to the mysql database
      public function importTable($table,$mdb,$db)
          // get the data
          $command = "mdb-export -D %F -R ';' -I mysql ". path('storage') . "database/" . $mdb . " '" . $table . "' 2>&1"; 
          $data = "USE '{$db}';" . shell_exec($command);
          file_put_contents(path('storage')."database/tabledata.txt", $data);
          //write file to mysql
          $command = "mysql -udbuser -premoved < " . path('storage') . "database/tabledata.txt 2>&1";

I am using this for connection

$dbe_name = path('storage')."database/events.mdb";

$driver = 'MDBTools';
$dataSourceName = "odbc:Driver=$driver;DBQ=$dbe_name;";
$dbe = new PDO($dataSourceName);

But apache is crashing out when I try a prepared statement.

Have another answer? Share your knowledge.