Cannot execute queries while other unbuffered queries are active

Cannot execute queries while other unbuffered queries are active

Can’t execute subsequent query after multiple queries from same Pdo_Mysql->exec() call

Can’t execute multiple stored procedures with Pdo_Mysql

Using the following code found in the Zend Framework Quickstart: Create a Model and Database Table

// this block executes the actual statements that were loaded from 
// the schema file.
try {
    $schemaSql = file_get_contents('./schema.sqlite.sql');
    // use the connection directly to load sql in batches
    $dbAdapter->getConnection()->exec($schemaSql);
    echo PHP_EOL;
    echo 'Database Created';
    echo PHP_EOL;

    if ($withData) {
        $dataSql = file_get_contents('./data.sqlite.sql');
        // use the connection directly to load sql in batches
        $dbAdapter->getConnection()->exec($dataSql);
        echo 'Data Loaded.';
        echo PHP_EOL;
    }

} catch (Exception $e) {
    echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
    echo $e->getMessage() . PHP_EOL;
    return false;
}

Note that $dbAdapter->getConnection()->exec() gets called twice.

I encountered this issue when calling Pdo_Mysql->exec() or in the Zend Framework calling $dbAdapter->getConnection()->exec($schemaSql) one after the other.

One way to prevent the error was to call $dbAdapter->closeConnection() after each exec call. Another way I worked around this problem was to just execute the $schemaSql and the $dataSql in one exec call like below;

$schemaSql = file_get_contents('schema.sql', 1);
$dataSql = file_get_contents('data.sql', 1);
// use the connection directly to load sql in batches
$dbAdapter->getConnection()->exec($schemaSql . $dataSql);
Bookmark and Share

About admin

Freelance Web Developer C# .NET, PHP5 Zend Framework, Flex/Actionscript
This entry was posted in Programming and tagged , , . Bookmark the permalink.

2 Responses to Cannot execute queries while other unbuffered queries are active

  1. Gins Babu says:

    Hi,,

    Thanks a lot Stormwild…………….i was looping around this issue for YEARS….thanks again….

  2. Pingback: China News » Blog Archive » unbuffered queries are active问题

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>