问题
I have to establish a connection via PHP to a Access Database, but everytime I try I get this error:
Fatal error: Uncaught exception 'com_exception'
With message:
'Source: Microsoft OLE DB
Service Components
Description: Das Format der Initialisierungszeichenfolge entspricht nicht den OLE DB-Angaben.' in E:\path\to\phpfile.php:93
Stack trace: #0 E:\path\to\phpfile.php(93): com->Open('DRIVER={Microso...') #1 {main} thrown in
The German text means something like "the format of the initialization string is not common of the OLE DB statements.
My connection code looks like
$db = 'DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\") & ("\\db\\db.mdb") & ";"';
$conn = new COM("ADODB.Connection");
$conn->Open($db);
I can't figure out what's wrong. In pure ASP it works fine, but not in PHP.
Thanks in advance
回答1:
Instead of using the "ADODB.Connection" module use PDO since it's currently the best option out there and is a best practice, Here is the documentation: http://php.net/manual/en/book.pdo.php
But if PDO isn't for you then see ODBC (PDO RECOMMENDED) which is a native PHP module. Here is the documentation: http://php.net/manual/en/book.uodbc.php
To start a connection you would do this: $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
回答2:
Use PDO instead. It can connect via ODBC:
$pathToMDB = "path/to/db.mdb";
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$pathToMDB", 'admin');
来源:https://stackoverflow.com/questions/14712661/cant-connect-through-php-to-an-access-database