phpsql-serverfreetdspdo-odbc

Invalid cursor state - pdo_odbc FreeTDS SQL Server


We're running a completely nonchalant query and are getting the following error:

PDOException : SQLSTATE[24000]: Invalid cursor state: 0 [FreeTDS][SQL Server]Invalid cursor state (SQLExecute[0] at /builddir/build/BUILD/php-5.5.13/ext/pdo_odbc/odbc_stmt.c:254)

From PHP we're using the pdo_odbc library to connect via FreeTDS to SQLServer 2008


Solution

  • It transpires that it was the preceding query that was causing problems. We were using:

    $pdo_statement->fetch(\PDO::FETCH_ASSOC);
    

    to get out a single row but not doing anything with it after then. This was leaving a cursor open which was then throwing the error then next time any query was executed. For us, closing the cursor before the next query was the way forward.

    $pdo_statement->closeCursor();
    

    It's also worth noting that using fetchAll() (in the preceding query) is also a fix as this does not leave a cursor open.

    $pdo_statement->fetchAll(\PDO::FETCH_ASSOC);