Yii - create temporary table and using it in next query produces General error: 2014 Cannot execute queries while other unbuffered queries are active
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开发者_如何学JAVA::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?
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
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' )";
}
精彩评论