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.

8
  • Have your script echo (8 * PHP_INT_SIZE) . "-bit<br/>";. The result ("32-bit" or "64-bit") must match the version of the Access Database Engine you installed. Commented Oct 4, 2014 at 14:28
  • it is giving me 32-bit as output. It should be 64 bit?? Commented Oct 4, 2014 at 15:33
  • Hmmm. An answer here 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")? Commented Oct 4, 2014 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" Commented Oct 4, 2014 at 15:56
  • 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. Commented Oct 4, 2014 at 16:24

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to get 64 bit version of php?
@user3933143 It's certainly possible, but the main PHP for Windows download page here 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.