penpen
penpen

Reputation: 935

Connect to Access with PHP

I need to connect to an Access Database through PHP.

I've uncommented "extension=php_pdo_odbc.dll" in the php.ini file. I've put the database in the same folder as the .php file I'm trying to connect with.

My code so far:

$dbName = "data.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName");

My error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] SQLDriverConnect: -1811 [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.' in C:\xampp\htdocs\Test\index.php:40 Stack trace: #0
C:\xampp\htdocs\Access_Test\index.php(40): PDO->__construct('odbc:DRIVER={Mi...') #1 {main} thrown in C:\xampp\htdocs\LAWsites\Access_Test\index.php on line 40

Any ideas?

Upvotes: 2

Views: 6567

Answers (2)

Robert Holland
Robert Holland

Reputation: 113

I was getting a similar error when the access database was located in the same folder.

SQLSTATE[HY000] SQLDriverConnect: -1044 [Microsoft][ODBC Microsoft Access Driver] Not a valid file name.

I put "./" in front of the filename and it worked.

$handler = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=./CIDB.MDB;");

Upvotes: 0

losnir
losnir

Reputation: 1171

The Database Location parameter (DBQ) requires an absolute path to your database file:

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath($dbName));

Upvotes: 4

Related Questions