Home > Backend Development > PHP Tutorial > PHP 讀取 Access mdb 檔

PHP 讀取 Access mdb 檔

WBOY
Release: 2016-06-23 13:14:06
Original
2845 people have browsed it

PHP 可透過 ODBC 存取 Access 資料庫檔。

環境 ODBC for Access MDB 設定:

1. Windows

在 windows 上,在 php.ini 開啟 php_pdo_odbc.dll 擴展

extension=php_pdo_odbc.dll
Copy after login

2. Linux

在 Linux 上,則要另外安裝 ODBC 和 mdbtools

[ mdbtools 說明 ]

mdbtools 是目前 Linux 上操作 Access MDB 免費方案的解決方法,

但我測試時 的版本 ,只能執行簡單的SQL語法,一些 SQL 語法在 windows 上正常,

但在 linux 透過 mdbtools 都無法執行(PHP PDO 執行後 false,直接使用指令透過 ODBC 操作,則出現 Couldn't parse SQL 的錯誤)。

安裝 php-odbc

$ yum install php-odbc
Copy after login

安裝 MDBTools

$ yum install mdbtools
Copy after login

安裝 mdbtools-devel ,安裝這個才有 libmdbodbc.so

$ yum install mdbtools-devel
Copy after login

查看 /etc/odbcinst.ini 內容,看一下設定的格式

$ less /etc/odbcinst.ini# Example driver definitions# Driver from the postgresql-odbc package# Setup from the unixODBC package[PostgreSQL]Description     = ODBC for PostgreSQLDriver          = /usr/lib/psqlodbcw.soSetup           = /usr/lib/libodbcpsqlS.soDriver64        = /usr/lib64/psqlodbcw.soSetup64         = /usr/lib64/libodbcpsqlS.soFileUsage       = 1# Driver from the mysql-connector-odbc package# Setup from the unixODBC package[MySQL]Description     = ODBC for MySQLDriver          = /usr/lib/libmyodbc5.soSetup           = /usr/lib/libodbcmyS.soDriver64        = /usr/lib64/libmyodbc5.soSetup64         = /usr/lib64/libodbcmyS.soFileUsage       = 1
Copy after login

可知要有 ODBC for MDB 的話, lib、lib64 目錄,應該會有 libmdbodbc.so 之類的檔案

$ ls -al /usr/lib/libmdb*ls: 無法存取 /usr/lib/libmdb*: 沒有此一檔案或目錄$ ls -al /usr/lib64/libmdb*-rwxr-xr-x 1 root root 38240  2月 20  2015 /usr/lib64/libmdbodbc.so-rwxr-xr-x 1 root root 42536  2月 20  2015 /usr/lib64/libmdbodbcW.solrwxrwxrwx 1 root root    15  2月  5 17:14 /usr/lib64/libmdb.so -> libmdb.so.2.0.1lrwxrwxrwx 1 root root    15  2月  5 16:35 /usr/lib64/libmdb.so.2 -> libmdb.so.2.0.1-rwxr-xr-x 1 root root 90104  2月 20  2015 /usr/lib64/libmdb.so.2.0.1lrwxrwxrwx 1 root root    18  2月  5 17:14 /usr/lib64/libmdbsql.so -> libmdbsql.so.2.0.0lrwxrwxrwx 1 root root    18  2月  5 16:35 /usr/lib64/libmdbsql.so.2 -> libmdbsql.so.2.0.0-rwxr-xr-x 1 root root 41144  2月 20  2015 /usr/lib64/libmdbsql.so.2.0.0
Copy after login

可發現 usr/lib64/ 底下有 libmdbodbc.so 的檔案link

在 odbcinst.ini 裡面,多加一組 MDBTools MDB ODBC Driver(名稱可自訂) 設定

$ vi /etc/odbcinst.ini[MyMDBDriver]Description     = MDBTools MDB ODBC DriverDriver          = /usr/lib/libmdbodbc.soSetup           = /usr/lib/libmdbodbc.soDriver64        = /usr/lib64/libmdbodbc.soSetup64         = /usr/lib64/libmdbodbc.soFileUsage       = 1UsageCount  = 1
Copy after login

PHP 程式範例:

$uname = explode(" ", php_uname());$os = $uname[0];switch ($os) {    case 'Windows':        $mdb_file = 'D:\test.mdb';        $driver = '{Microsoft Access Driver (*.mdb)}';        break;    case 'Linux':        $mdb_file = '/home/test.mdb';        $driver = 'MyMDBDriver'; //在 /etc/odbcinst.ini 裡的設定名稱        break;    default:        exit("Don't know about this OS");}$dataSourceName = "odbc:Driver=$driver;DBQ=$mdb_file;";$connection = new PDO($dataSourceName);$sql = "SELECT * FROM test";$stmt = $connection->query($sql);$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
Copy after login

參考:

Access a mdb database from a php web site

How to handle MS Access MDB files in Linux with PHP5 PDO and ODBC · GitHub

PHP: PDO_ODBC DSN - Manual

其他:

1. 若在 /etc/odbc.ini 增加以下設定

$ vi /etc/odbc.ini[MDBtest]Description = The Source of My test mdbDriver      = MyMDBDriverServerName  = localhostDatabase    = /home/test.mdb
Copy after login

則可這樣進入 SQL 指令介面,進入後,可直接用 SQL 指令操作 /home/test.mdb 資料庫

$ isql -v MDBtest+---------------------------------------+| Connected!                            ||                                       || sql-statement                         || help [tablename]                      || quit                                  ||                                       |+---------------------------------------+SQL>
Copy after login

2. 編譯安裝 mdbtools:

GitHub - brianb/mdbtools: MDB Tools - Read Access databases on *nix

Install mdbtools on Centos 7 – Dale Scott

tar zxvf mdbtools-0.6pre1.tar.gzcd mdbtools-0.6pre1/autoreconf -i -f./configure --with-unixodbc=/usr/localmakemake install
Copy after login

3. Access mdb 時間欄位:

SQL語法中條件值中有日期時間格式的值﹐需要在日期時間值的左右加上#

$sql = "SELECT * FROM test WHERE aa >#{2016-03-15}#";
Copy after login

參考: 請問mdf與mdb的SQL語法有何差別? - Delphi K.Top 討論區

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template