I am unable to connect to access database through php. I have searched the internet for the issue. It is my 3rd day, I am trying this but could not find the solution. I have installed all the required things: ODBC drivers and MS Access Database engine.
My php code to connect to database named newDB.mdb is:
<?php
$dbName = $_SERVER["DOCUMENT_ROOT"] . "access_db/newDB.mdb";
echo $dbName."<br />";
if (!file_exists($dbName)) {
die("Could not find database file.<br />".$dbName);
}
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=$dbName");
} catch(PDOException $e) {
echo "Error: ".$e->getMessage()."<br />";
}
?>
The error I receive:
C:/wamp/www/access_db/newDB.mdb
Error: SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager]
>Data source name not found and no default driver specified
I am working on a local machine.
OS: Windows8 Pro, and WampServer 2.5, Microsoft Access 2013 (I am exporting the file as .mdb, it also doesn't work for .accdb extension)
I don't know what I am lacking, or what I need to do.
Your driver name is missing a space. It should be
DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}
^
As long as just needing to support .mdb-files, you can try this:
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};Dbq=$dbName");
This worked fine for me.
It seems, on default the "Microsoft Access Driver (*.mdb, *.accdb)" is not installed. Open C:\Windows\SysWOW64\odbcad32.exe
in the register card Drivers
.
In my case - I think that's the standart office/access installation behavior - ther was just the Microsoft Access Driver (*.mdb)
.
If you need to support .accdb-files, you'll need to install the "Microsoft Access Database Engine 2010 Redistributable": https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=13255
After installing, you can use
$db = new PDO("odbc:DRIVER={Microsoft Access Driver
(*.mdb,*.accdb)};Dbq=$dbName");
Edit:
I found a newer version for my installation of Office 2016 (https://www.microsoft.com/en-us/download/details.aspx?id=54920) but was unable to install. I got errors reporting for using 64-bit office components, but 32-bit office is installed. Anybody an idea to solve the problem?