2

大家好,我想使用 PHP、ODBC 或任何其他数据库连接来连接受 USERNAME、PASSWORD 保护的 MS ACCESS 2000 (.*mdb) 数据库。但是下面给出的代码显示警告并且不连接到数据库。请任何人帮助。

代码 1

$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$user = 'test';
$password ='123';
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename;",$user,$password);

if($connection)
{
    echo "Success";
}
else
{
    exit("Connection Failed: ".$connection);
}

警告信息

Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You do not have the necessary permissions to use the '(unknown)' object. Have your system administrator or the person who created this object establish the appropriate permissions for you., SQL state 42000 in SQLConnect in C:\xampp\htdocs\punch\test.php on line 6

输出 -> 连接失败

代码 2

我也尝试了下面的代码。没有任何错误或警告消息,但未连接到访问数据库。

$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$systemDbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\SYSTEM.MDW";
$user = 'test';
$password ='123';

$adoCon = new COM("ADODB.Connection") or die("Cannot start ADO");
$connection= $adoCon->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename; SystemDB=$systemDbFilename;Uid=$user; Pwd=$password;");

if(($connection->State) > 0)
{
    echo "Success";
}
else
{
    exit("Connection Failed: ".$connection);
}

输出 -> 连接失败


正确答案

最后我找到了答案。我尝试使用 PDO 连接 ODBC。

工作代码

try {
    $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Door\DB\IMSDB.mdb;SystemDB=D:\Door\DB\SYSTEM.MDW;Uid=test;Pwd=123;");

    if($dbh)
    {
        echo "Connection Success";
    }
}
catch (PDOException $e)
{
    echo $e->getMessage();
}
4

0 回答 0