无法使用PHP读取MS Access表(链接到ODBC数据源)

I am working on an application in which the data from an ODBC enabled Data Source is imported to an MS Access 2007 Database using a Linked Table. The tricky part is that every time a Link Refresh is made (or the linked table is opened) the ODBC Data Source prompts out a Window where 4 different parameters (username, password, servername, workgroupname) have to be input.

While this is possible manually, I find no way to read this linked Table programatically.

I am using a PHP Script to read the linked table. The Execution of PHP Script is halted indefinitely when I try to read this linked table. My DSN is properly configured and can successfully read the data in all other cases including those linked tables in which the data source doesnot require any input parameters/credentials. However, it fails in this case.

In a nutshell, my system is PHP<-->MS Access (Linked Table)<--ODBC Data Source. The PHP Script aims to read the updated data from the ODBC Data Source using the Linked Table. For some reasons, PHP cannot directly interface with the ODBC Data Source, so I am using MS Access as an intermediary.

The odbc_connect function of PHP allows only DSN Name, username, password, cursor_type as parameters. I am not sure how do I input the 4 parameters (username, password, servername, workgroupname) as required by the ODBC Data Source to which my MS Access table is linked.

I am using PHP 5.4 on Windows 7 with MS Access 2007.

I request the developer community to help me with necessary pointers on this. Any suggestions for Workaround is also welcome.

Thanks in Advance.

if you are using double ODBC: PHP > ODBC > MS ACCESS > ODBC > SOURCE.. you have following options.

  • Cut off the "Access" middle man and connect directly.
  • if you want to use MS Access, use "FILE DNS" to connect to the linked tables from MS ACCESS where file DNS has all 4 parameters initialized. (This will eliminate MS Access asking you to enter the server information when refreshing linked tables)
  • Haven't tried this from PHP side but you can loop through the msysobjects table or database.tabledefinitions and update the link with your custom connection string.

Any way, the solution for your trouble would be. Make Access not to prompt for parameters when refreshing linked tables by either using file dns or save passwords.

Connect directly to the data source where linked table derives. If that is MS Access, SQL Server, MySQL, etc. use the appropriate, installed ODBC driver for that particular database.

Here is a PHP/MS Access DSN-less solution using PDO.

Also, see different connection strings which includes workgroup, user, and password parameters.

<?php

$database="C:\Path\To\database.accdb";

# open the connection
try {
    $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                    DBq=$database;Uid=Admin;Pwd=;");
    $sql = "SELECT * FROM table1";    
    $STH = $dbh->query($sql);

    $STH->setFetchMode(PDO::FETCH_ASSOC); 
}
catch(PDOException $e) {  
    echo $e->getMessage()."
";
    exit;
}

echo "
";
while($row = $STH->fetch()) {
    # output query results            
    echo $row;
}

# close the connection
$dbh = null;

?>