PHP PDO,Ubuntu,MDBTools连接到Access数据库的问题

时间:2018-09-04 15:26:42

标签: php ubuntu pdo ms-access-2010 mdbtools

我无法调试与MSAccess 2010 .accdb文件的连接。

我正在运行Scotch Box Vagrant vm(v3.5),并已通过sudo apt-get install mdbtools安装了MDBTools odbc驱动程序。

我的/etc/odbcinst.ini文件如下:

# To enable unixODBC tracing, add this section to odbcinst.ini.
# Include the [ODBC] section heading.
[ODBC]
Trace = yes
TraceFile = /var/www/unixODBC.log

[MDBTools]
Description=MDBTools Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libmdbodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libmdbodbc.so
FileUsage=1
UsageCount=1

我还在/etc/odbc.ini中创建了一个数据源

[CONTACTS]
Driver = MDBTools
Description = Contacts Database
Database = /data/contacts-db.accdb
Server = localhost

我用来尝试连接的测试php文件称为testMSACCESS.php,看起来像这样:

<?php
try {
    $dbFile = '/data/contacts-db.accdb';
    echo 'Does file exist at ' . $dbFile . '? ';
    var_dump(file_exists($dbFile));
    var_dump(file_exists($dbFile));
    echo 'Is the file readable ? ';
    var_dump(is_readable($dbFile));
    echo 'Is the file writable ? ';
    var_dump(is_writable($dbFile));
    $dbh = new PDO("odbc:Driver=MDBTools;DBQ={$dbFile};");
    // or $dbh = new PDO("odbc:Driver=MDBTools;DSN=CONTACTS;");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($dbh){
        echo "success <br/>";
    }
    $dbh = null;
}catch (Exception $e){
    echo '<pre>';
    echo 'Exception caught: could not connect to database.' . PHP_EOL;
    echo (string) $e;
    echo '</pre>';
}

数据库文件contacts-db.accdb位于计算机/data根目录中。

当我尝试通过在浏览器中加载脚本进行连接时,抛出的异常如下所示:

Does file exist at /data/contacts-db.accdb? bool(true)
Is the file readable ? bool(true)
Is the file writable ? bool(true)
Exception caught: could not connect to database.
PDOException: SQLSTATE[00000] SQLDriverConnect: 0  in /var/www/public/testMSACCESS.php:6
Stack trace:
#0 /var/www/public/testMSACCESS.php(6): PDO->__construct('odbc:Driver=MDB...')
#1 {main}

通过cmd行运行脚本时,我得到了更多信息:

vagrant@scotchbox:~$ php /var/www/public/testMSACCESS.php 
Does file exist at /data/contacts-db.accdb? bool(true)
Is the file readable ? bool(true)
Is the file writable ? bool(true)
Unknown Jet version.
File not found
Unable to locate database /data/contacts-db.accdb
<pre>Exception caught: could not connect to database.
PDOException: SQLSTATE[00000] SQLDriverConnect: 0  in /var/www/public/testMSACCESS.php:6
Stack trace:
#0 /var/www/public/testMSACCESS.php(6): PDO->__construct('odbc:Driver=MDB...')
#1 {main}</pre>

这是unixODBC跟踪日志的样子:

[ODBC][1515][1536073973.983218][__handles.c][460]
        Exit:[SQL_SUCCESS]
            Environment = 0x5591139d5d60
[ODBC][1515][1536073973.985108][SQLSetEnvAttr.c][189]
        Entry:
            Environment = 0x5591139d5d60
            Attribute = SQL_ATTR_ODBC_VERSION
            Value = 0x3
            StrLen = 0
[ODBC][1515][1536073973.985588][SQLSetEnvAttr.c][363]
        Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.990811][SQLSetEnvAttr.c][189]
        Entry:
            Environment = 0x5591139d5d60
            Attribute = SQL_ATTR_CP_MATCH
            Value = (nil)
            StrLen = 0
[ODBC][1515][1536073973.991928][SQLSetEnvAttr.c][363]
        Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.992707][SQLAllocHandle.c][375]
        Entry:
            Handle Type = 2
            Input Handle = 0x5591139d5d60
[ODBC][1515][1536073973.993173][SQLAllocHandle.c][493]
        Exit:[SQL_SUCCESS]
            Output Handle = 0x5591139d6400
[ODBC][1515][1536073973.993682][SQLSetConnectAttr.c][396]
        Entry:
            Connection = 0x5591139d6400
            Attribute = SQL_ATTR_AUTOCOMMIT
            Value = 0x1
            StrLen = -6
[ODBC][1515][1536073973.994129][SQLSetConnectAttr.c][681]
        Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.994892][SQLSetConnectAttr.c][396]
        Entry:
            Connection = 0x5591139d6400
            Attribute = SQL_ATTR_ODBC_CURSORS
            Value = (nil)
            StrLen = -6
[ODBC][1515][1536073973.995351][SQLSetConnectAttr.c][681]
        Exit:[SQL_SUCCESS]
[ODBC][1515][1536073973.995872][SQLDriverConnect.c][726]
        Entry:
            Connection = 0x5591139d6400
            Window Hdl = (nil)
            Str In = [Driver=MDBTools;DBQ=/var/www/public/contacts-db.accdb;][length = 54]
            Str Out = 0x7ffdb1be6d00
            Str Out Max = 1023
            Str Out Ptr = 0x7ffdb1be6cf8
            Completion = 0
        UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

[ODBC][1515][1536073974.001199][SQLDriverConnect.c][1353]
        Exit:[SQL_ERROR]
[ODBC][1515][1536073974.001731][SQLGetDiagRec.c][680]
        Entry:
            Connection = 0x5591139d6400
            Rec Number = 1
            SQLState = 0x7f11dce6c010
            Native = 0x7f11dce6c218
            Message Text = 0x7f11dce6c016
            Buffer Length = 511
            Text Len Ptr = 0x7ffdb1be688a
[ODBC][1515][1536073974.002167][SQLGetDiagRec.c][717]
        Exit:[SQL_NO_DATA]
[ODBC][1515][1536073974.002707][SQLEndTran.c][421]
        Entry:                
            Connection = 0x5591139d6400                
            Completion Type = 1
[ODBC][1515][1536073974.003125][SQLEndTran.c][433]Error: 08003
[ODBC][1515][1536073974.003551][SQLDisconnect.c][208]
        Entry:
            Connection = 0x5591139d6400
[ODBC][1515][1536073974.004006][SQLDisconnect.c][237]Error: 08003
[ODBC][1515][1536073974.004462][SQLFreeHandle.c][284]
        Entry:
            Handle Type = 2
            Input Handle = 0x5591139d6400
[ODBC][1515][1536073974.004999][SQLFreeHandle.c][333]
        Exit:[SQL_SUCCESS]
[ODBC][1515][1536073974.005544][SQLFreeHandle.c][219]
        Entry:
            Handle Type = 1
            Input Handle = 0x5591139d5d60

我可以在跟踪[SQLDriverConnect.c][726][SQLDriverConnect.c][1353]中看到故障发生的位置。它似乎是连接错误,但是我无法理解跟踪日志[SQLGetDiagRec.c][680]的以下块中包含的信息。

此外,我不明白为什么PHP无法找到数据库,或者为什么它说它不知道Jet Engine版本,因为该文件是使用8岁的MS版本创建的Access(2010)。

如果有人对如何调试错误有任何想法,我将不胜感激。

0 个答案:

没有答案