Can't connect through PHP to an Access Database

╄→гoц情女王★ 提交于 2019-12-24 19:45:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!