简体   繁体   中英

Connecting to a MS Access database from PHP on Linux

My client has been running a Windows server for years but we are now moving to a separate Linux machine for the web app I have created for them. Currently we run PHP on the Windows server on which we are able to connect to an MDB file that is on the same disk. This is a file from an external party, the web app uses MySQL. In the new setup we have a Linux web server (Apache/MySQL/PHP) and a Windows 2016 server which are connected via VPN and we have mounted a share on the Windows server in which the MDB file is located. So far, so good, however I can't seem to query the MDB file. The connection is made, not error there, but every query I run returns an error or nothing not sure. This is my code:

<?php
$db=new PDO("odbc:Driver=MDBTools; DBQ=/mnt/<dir>/<file>.mdb;");
$query=$db->query("SELECT * FROM <table>;");

$return=array();
if($query) {
     while($result=$query->fetch(PDO::FETCH_ASSOC)) {
         $return[]=$result;
     }
}else $return['error']=1;

//close
$query=null;
$db=null;

print_r($return);
?>

Currently everything returns error > 1.

PDO throws the following error:

Connection failed: SQLSTATE[08001]: Client unable to establish connection: 1 Couldn't parse SQL (SQLExecute[1] at /build/php7.2-pRoOsC/php7.2-7.2.24/ext/pdo_odbc/odbc_stmt.c:260)

I found the solution to my problem was removing the ; from the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM