1

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.

Sam
  • 1,237
  • 1
  • 16
  • 29
  • Have your script `echo (8 * PHP_INT_SIZE) . "-bit
    ";`. The result ("32-bit" or "64-bit") must match the version of the Access Database Engine you installed.
    – Gord Thompson Oct 04 '14 at 14:28
  • it is giving me 32-bit as output. It should be 64 bit?? – Sam Oct 04 '14 at 15:33
  • Hmmm. An answer [here](http://stackoverflow.com/a/3233881/2144390) suggests that the above test *might* be misleading. Can you create an .mdb file for testing and try connecting to it using `Driver={Microsoft Access Driver (*.mdb)}` (without the ", *.accdb")? – Gord Thompson Oct 04 '14 at 15:49
  • getting different error "odbc_fetch_row() expects parameter 1 to be resource, array given in C:\wamp\www\test\new 1.php on line 15" – Sam Oct 04 '14 at 15:56
  • is this issue with wamp?? should i try some other server like xampp?? – Sam Oct 04 '14 at 15:57
  • It's really a question of whether PHP is running as a 64-bit process or a 32-bit process. I suppose it's possible that a 64-bit WampServer install could really be running PHP as 32-bit (for whatever reason). In fact, that *appears* to be the case if you didn't get a connection error when you tried `Driver={Microsoft Access Driver (*.mdb)}` because that will definitely fail if PHP is running as 64-bit. – Gord Thompson Oct 04 '14 at 16:06
  • is it possible to see whether php is 32 bit or 64 bit? Also i have 64 bit os/ 64 bit office/ 64 bit wamp. Should everything be 64 bit or 32 bit?? – Sam Oct 04 '14 at 16:09
  • 1
    Aha. I'm pretty sure you are getting the *"odbc_fetch_row() expects parameter 1 to be resource ..."* error because of `$rs[]=odbc_exec($conn,$sql);`. Try your .mdb test with `$rs=odbc_exec($conn,$sql);` instead. – Gord Thompson Oct 04 '14 at 16:24

1 Answers1

2

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.
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • is it possible to get 64 bit version of php? – Sam Oct 04 '14 at 16:55
  • @user3933143 It's certainly possible, but the main PHP for Windows download page [here](http://windows.php.net/download/) says "**Note:** x64 builds are currently **experimental**". (Perhaps that's why you got a 32-bit version in the first place.) There's also the question of how to "drop" another version of PHP into an existing WAMP install. Unfortunately I have no experience with that. – Gord Thompson Oct 04 '14 at 17:18