简体   繁体   中英

Using PHP to connect to MS Access DB using ODBC keeps on locking

I know this is not the best setup, but I'm stuck with it and cannot change it.

PHP connects to a local MS Access DB (.mdb) to log certain activities being done by the script. This works fine when only one instance of the script is running. However, if run two instances, I occasionally get the following error:

odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Could not update; currently locked., SQL state S1000 in SQLExecDirect

This only happens intermittently so I'm assuming it's only when the two scripts both happen to be trying to write to the same table at the same time. I never had this issue with other DBs such as MySQL. How can I tell PHP/ODBC/Access to either not lock or try again if it is locked?

I also usually have the Access DB open on my screen for troubleshooting purposes.

odbc_exec() returns FALSE if the query fails, so you could just check the result of the query and keep trying until it succeeds:

$sql = "UPDATE [Clients] SET [Position]='somewhere' WHERE [ID]=1";
while (TRUE) {
    $result = odbc_exec($conn, $sql);
    if ($result) {
        break;
    }
    else {
        $lastError = odbc_error();
        if ($lastError != "S1000") {
            echo "ODBC error $lastError";
            break;
        }
    }
    sleep(1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM