0

I am new to PHP as well as new to Ms-access database. I am trying to connect php server to ms access database dsnless as i have to give this to my friend. My both php file and ms database file are in same directory and they both are going to stay on localhost only. I am not able to understand the error.

I have 64 bit wamp version 2.4/Apache Version :2.4.4/PHP Version :5.4.16/ 64 bit windows 7 Also i have installed Microsoft Access Database Engine 2010 Redistributable from official site.

Here is my php code

<?php
echo (8 * PHP_INT_SIZE) . "-bit<br/>";
$user = "";
$password = "";
$mdbFilename= "C:\wamp\www\test\testdb.accdb";

$conn=$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $user, $password);              ////<----line 10

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))
{
    $json_output[] = odbc_result($rs, "test");
    print(json_encode($json_output));

}
odbc_close($conn);
?>  

Here is my error

Warning: odbc_connect(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name., SQL state S1000 in SQLConnect in C:\wamp\www\test\working.php on line 10
Sam
  • 1,237
  • 1
  • 16
  • 29
  • 1
    Have you tried doubling up your '\' in the filespec. – bohica Oct 15 '14 at 09:26
  • Are you still running 32-bit PHP (as in your previous question [here](http://stackoverflow.com/q/26193598/2144390))? – Gord Thompson Oct 15 '14 at 10:03
  • @GordThompson Yes I am still running 32 bit version as 64 bit version is still experimental and not ready for commercial use. – Sam Oct 15 '14 at 13:03
  • @bohica Yes i did and this is how my path looked C:\\wamp\www\test\testdb.accdb but it didn't worked – Sam Oct 15 '14 at 13:04
  • 1
    Your example above only doubled the first \ and not all back slashes. – bohica Oct 15 '14 at 13:21
  • @bohica I did double all then backlash C:\\wamp\\www\\test\\test.mdb still it didn't work. – Sam Oct 15 '14 at 15:17

1 Answers1

0

This is what I'm using to connect with a Ms-Access:

<?php
    $conn = new COM("ADODB.Connection");
    $dns = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath("./youraccessdb.mdb").";";
    $conn->open($dns);
    $rs = $conn->execute("SELECT * FROM table");
    $field =  $rs->Fields(0);

    while (!$rs->EOF) {
      print $f1->value . "\n";
      $rs->MoveNext();
    }
    $rs->Close();
    $conn->Close();
?>

Hope this help.

Mauro Casula
  • 68
  • 1
  • 7
  • I am getting this error Fatal error: Class 'COM' not found in C:\wamp\www\test\test3.php on line 4 – Sam Oct 15 '14 at 06:54
  • You have to add `extension=php_com_dotnet.dll` to your php.ini to load the com/dotnet module. – Mauro Casula Oct 15 '14 at 07:06
  • I did that and now i am getting this error Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft OLE DB Provider for ODBC Drivers
    Description: Operation was canceled.' in C:\wamp\www\test\test3.php on line 6
    – Sam Oct 15 '14 at 07:28
  • i am getting two errors and this is second error com_exception: Source: Microsoft OLE DB Provider for ODBC Drivers
    Description: Operation was canceled. in C:\wamp\www\test\test3.php on line 6
    – Sam Oct 15 '14 at 07:29
  • this is line 6 $conn->open($dns); – Sam Oct 15 '14 at 07:29
  • Are you using Apache or IIS? Are you sure that the path of your DB it's ok? – Mauro Casula Oct 15 '14 at 07:55
  • I am using 64-bit wamp server. Path is exactly where the database and php server files are(both in same folder) but the biggest issue is it works when i use dsn but it doesn't work when i use dsn-less. Don't know why – Sam Oct 15 '14 at 07:58
  • @user3933143 `DRIVER={Microsoft Access Driver (*.mdb)}` cannot read .accdb files. As the name indicates, it can only work with .mdb files. `DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}` can work with both. – Gord Thompson Oct 15 '14 at 13:23
  • @GordThompson I am getting same error even after changing to mdb and using new access files with mdb extension. – Sam Oct 15 '14 at 15:10