How to Access .mdb Data Source in PHP Linux?

核能气质少年 提交于 2020-06-16 16:59:52

问题


I just installed unixODBC in order to access my .mdb data source on PHP.5.3 CentOS 6.10 using this commands:

# yum install unixODBC  

In Windows i could simply uncomment extension=php_pdo_odbc.dll on php.ini and restart Httpd / Apache in order to access my .mdb, and then made connection thorugh PDO like this :

<?php

    $dsn = "{Microsoft Access Driver (*.mdb, *.accdb)}";
    $db = "/home/www/html/cetak_absensi/uploaded/db_access/myDB.mdb";
    $user = "";
    $pass = "";

    $connection = new PDO("odbc:DRIVER=".$dsn.";charset=UTF-8; Dbq=".$db."; Uid=".$user."; Pwd=".$pass.";");
    ...
    ...
?>

After installed unixODBC and PDO-ODBC already running on my php, i run my code above, unfortunately i face this error :

[unixODBC][Driver Manager]Data source name not found, and no default driver specified php linux

After searched on another post here, i think my DSN is not correct.

What is the correct DSN for this in Linux ?

Is that really possible to access my .mdb data source through PHP-PDO in Linux especially CentOS 6.10?

UPDATE

After searched i have found an article explained about this. And i installed libmdodbc1 from this resource libmdbodbc and add following line in /etc/odbcinst.ini :

# Source Came From:
# 1. https://centos.pkgs.org/6/forensics-x86_64/libmdbodbc1-0.7-43.13.el6.x86_64.rpm.html
# 2. https://gist.github.com/amirkdv/9672857
# etc.
[MDBTools]
Description = MDBTools Driver
Driver64      = /usr/lib64/libmdbodbc.so.1.0.0
Setup64       = /usr/lib64/libmdbodbc.so.1.0.0
FileUsage   = 1
UsageCount  = 1

And then i change the connection like this:

connection.php

<?php
    /*
    ini_set('upload_max_filesize', '100M');
    ini_set('post_max_size', '100M');
    echo ini_get('upload_max_filesize'), ", " , ini_get('post_max_size');
    echo php_uname();
    */
    set_time_limit(600);

    $query = 'SELECT * FROM LEAVECLASS1';
    $mdb_file = '/var/www/html/cetak_absensi/uploaded/db_access/db_absensi_backup.mdb';
    $uname = explode(" ",php_uname());
    print_r($uname);
    $os = $uname[0];
    echo "<br>";
    echo $os;
    switch ($os){
      case 'Windows':
        $driver = '{Microsoft Access Driver (*.mdb)}';
        break;
      case 'Linux':
        $driver = 'MDBTools';
        break;
      default:
        exit("Don't know about this OS");
    }
    $dataSourceName = "odbc:Driver=$driver;DBQ=$mdb_file;";
    echo "<br>";

    try {
        $connection = new \PDO($dataSourceName);
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $result = $connection->prepare($query);
        $result->execute();
        $data = $result->fetchAll(\PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        echo "<br><br>";
        echo "There is an error " . $e->getMessage();
        echo "<br><br>";
    }

    // I think this following code cause this error:
    //$result = $connection->query($query)->fetchAll(\PDO::FETCH_ASSOC);

    echo "<br>";
    print_r($data);
    echo "<br>";
    var_dump($connection);

After run this code the browser said :

The connection was reset

I have increased my memory_limit and max_execution_time in php.ini but still throw the same message.

Can you tell me why the browser throw this message ?

Maybe i have set wrong configuration or my .mdb data is too large.

UPDATE 2

I uninstalled my libmdodbc1 (my previous libmdodbc on first update above). And then installed libmdodbc1 using $ sudo yum install libmdodbc1 and my configuration in /etc/odbcinst.ini is still same like previous (same as on first update above) and then restarted the service. My reference is this article How to handle MS Access MDB files in Linux with PHP5 PDO and ODBC.

Furthermore i changed $mdb_file variable to $mdb_file = '/var/www/html/cetak_absensi/uploaded/db_access/db_new.mdb' that only has a table with 3 records in it. And also i added var_dump($data); to see the fetched data.

db_new.mdb looks like this:

ID  Nama Depan     Nama Belakang      Pekerjaan
1   First Name 1    Last Name 1       Programmer
2   First Name 2    Last Name 2       Guru
3   First Name 3                      Dokter

After i ran connection.php, var_dump($connection); and var_dump($data) respectively result :

// var_dump($connection);
object(PDO)#1 (0) { }

// var_dump($data);
array(2) { [0]=> array(4) { ["ID"]=> string(1) "2" ["Nama Depan"]=> NULL ["Nama Belakang"]=> NULL ["Pekerjaan"]=> NULL } [1]=> array(4) { ["ID"]=> string(1) "3" ["Nama Depan"]=> NULL ["Nama Belakang"]=> NULL ["Pekerjaan"]=> NULL } }

(END OF FIRST TRIAL).

Why $data only has fieldnames with NULLs value ?

Thought the connection already made it, i tested with my previous .mdb file (db_absensi_backup.mdb) and the page still showing the same issue.

The connection was reset

And the error_log said:

[Tue Jun 02 14:47:12 2020] [notice] child pid 5366 exit signal Segmentation fault (11)

(END OF SECOND TRIAL).

The first trial on this second update is almost made it, while the second trial with my real .mdb file seems like the package is out of date.

Can you tell me why these happened ?

And are these because i made wrong configuration before ?

来源:https://stackoverflow.com/questions/61813881/how-to-access-mdb-data-source-in-php-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!