PHP MS Access连接不起作用

问题描述:

我正在尝试将php服务器连接到ms Access数据库,并且我尝试了所有仍然无法连接的东西.

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

这是我的代码

<?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);
?>  

如果我使用

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

然后我得到以下错误

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

如果我使用

$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

我已经编辑了php.ini文件以包括odbc扩展名

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 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.

对不起,我的英语不好,我对php和连接ms访问都是陌生的.我已经完成了与mysql的连接,但从未访问过.

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.

测试确认,尽管已报告安装了64位的WampServer,PHP仍以32位进程运行.较旧的"Jet" ODBC驱动程序(Driver={Microsoft Access Driver (*.mdb)})可以成功读取.mdb文件,并且没有64位版本的Jet,因此PHP必须以32位版本运行.

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.

现在,安装了64位Office的问题是32位PHP将需要使用32位版本的较新的Access Database Engine(又名"ACE")驱动程序来处理.accdb文件,但是Microsoft在同一台计算机上不支持ACE的32位和64位版本. (通过网络搜索可以找到一种强制发生这种情况的方法,但是不建议这样做,因为它显然可以破坏Office.)

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:

  • 使用.mdb文件而不是.accdb文件,并在32位PHP下继续使用Jet,
  • 找到将PHP作为64位进程运行的WAMP设置,或者
  • 切换到32位版本的Office.