PHP MS Access connection not working

2019-07-25 06:00发布

I am trying to connect php server to ms access database and i have tried everything still i am not able to connect.

Here is my code

<?php
$conn=odbc_connect('testdb','','');
//$conn=odbc_connect("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\wamp\www\test\testdb.accdb", '', '');
if (!$conn) {
  exit("Connection Failed: " . $conn);
}

$sql="SELECT * FROM testdb";
$rs[]=odbc_exec($conn,$sql);
if (!$rs) {
  exit("Error in SQL");
}

while (odbc_fetch_row($rs))    //<-------line 14
{
    $json_output[] = odbc_result($rs, "test");
    print(json_encode($json_output));

}
odbc_close($conn);
?>  

If i use

 $conn=odbc_connect('testdb','','');

then i get following error

Warning: odbc_fetch_row() expects parameter 1 to be resource, array given in C:\wamp\www\test\new 1.php on line 14

if I use

$conn=odbc_connect("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\wamp\www\test\testdb.accdb", '', '');

then i get below line as error.

Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\wamp\www\test\new 1.php on line 3

I've edited my php.ini file to include the odbc extension

;extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
extension=php_pdo_odbc.dll  <--- here
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll

Also i have have downloaded and installed Microsoft Access Database Engine 2010 Redistributable from this link.

Also i did try everything that is shown in this video.

I have also done exactly that is written in the accepted answer in this link and i am running 64-bit WampServer Version 2.4 on windows 7 64 bit and also have 64 bit microsoft office.

Sorry for my bad english and i am new to both php and connecting to ms access. I have done connecting to mysql but never to access.

1条回答
Animai°情兽
2楼-- · 2019-07-25 06:10

Testing confirmed that despite a reported 64-bit install of WampServer, PHP was running as a 32-bit process. The older "Jet" ODBC driver (Driver={Microsoft Access Driver (*.mdb)}) could successfully read an .mdb file and there is no 64-bit version of Jet, so PHP must be running as 32-bit.

Now, with 64-bit Office installed the issue is that 32-bit PHP will need to use a 32-bit version of the newer Access Database Engine (a.k.a. "ACE") driver to manipulate an .accdb file, but Microsoft does not support both 32-bit and 64-bit versions of ACE on the same machine. (A web search will reveal that there is a way to force that to happen, but it is not recommended because it can apparently break Office.)

So, the ultimate resolution would be one of the following:

  • use an .mdb file instead of an .accdb file and continue using Jet under 32-bit PHP,
  • find a WAMP setup that runs PHP as a 64-bit process, or
  • switch to the 32-bit version of Office.
查看更多
登录 后发表回答