且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过PHP连接到Access数据库

更新时间:2022-10-18 18:02:02

使用PDO代替使用"ADODB.Connection"模块,因为它是目前的***选择,也是一种***实践,这里是文档: http://php.net/manual/zh/book.pdo.php >

但是,如果不适合使用PDO,则可以看到ODBC(推荐使用PDO),这是一个本机PHP模块.这里是文档: http://php.net/manual/zh/book.uodbc. php
要开始连接,请执行以下操作:$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

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

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);