Yii - create temporary table and using it in next query produces General error: 2014 Cannot execute queries while other unbuffered queries are active

泪湿孤枕 提交于 2020-01-01 19:06:02

问题


I am creating temporary table to hold some dates in first query. And in second query I try to join with those dates... and than i get following error:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute..

First query:

    $query = "DROP TABLE if exists TempDatesTable;";
    $query .= "CREATE TEMPORARY TABLE TempDatesTable ( days char(20) ) TYPE=HEAP  DEFAULT CHARSET=utf8;";

    foreach ($allDatesInsideInterval as $date) {
        $query .= "INSERT INTO TempDatesTable VALUES( '$date' );";
    }

    Yii::app()->db->createCommand($query)->execute();

Second query

$command = Yii::app()->db->createCommand()
                    ->select('allDays.days as periodDay, numberOfSentRequests, numberOfReceivedRequests, numOfLogins, numOfProfilesViewed')
                    ->from("(" . $commandDates->getText() . ") allDays")
                    ->leftJoin("(" . $commandProfileViewed->getText() . ") accessLog", 'allDays.days = accessLog.days')....

When I try to run second query:

return new CSqlDataProvider($command->getText(), array(
        'totalItemCount' => count($allDatesInsideInterval),
        'pagination' => array(
            'pageSize' => self::PAGE_SIZE
        ),
        ...

I have seen that I need to do fetchAll(); and closeCursor(); ... but how to do it in Yii? Any ideas?


回答1:


After you execute and/or fetch your data for a query, try:

$command = false;

see: http://www.yiiframework.com/doc/guide/1.1/en/database.dao




回答2:


I got same error when I have ";" in query

Try change first query

$query = "DROP TABLE if exists TempDatesTable;";
$query .= "CREATE TEMPORARY TABLE TempDatesTable ( days char(20) ) TYPE=HEAP  DEFAULT CHARSET=utf8";

foreach ($allDatesInsideInterval as $date) {
    $query .= "; INSERT INTO TempDatesTable VALUES( '$date' )";
}


来源:https://stackoverflow.com/questions/6857378/yii-create-temporary-table-and-using-it-in-next-query-produces-general-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!