如何使用php连接用户名密码保护ms access 2000数据库(。* mdb)

时间:2013-07-09 04:48:15

标签: php ms-access pdo odbc ado

大家好我想使用PHP,ODBC或任何其他数据库连接连接USERNAME,PASSWORD受保护的MS ACCESS 2000(。* mdb)数据库。但是下面的代码显示了WARNING并且没有连接到数据库。请任何人帮忙。

代码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();
}

0 个答案:

没有答案