I'm using CakePHP and trying to retrieve FOUND_ROWS() for a query that runs several times in a loop (each time with a different WHERE section). The strangest thing is happening: it returns the correct result for the first query - and then the same exact result for subsequent queries.
This is the code:
$article = new Article();
$query = $db->buildStatement(
array(
'table' => $db->fullTableName($article)
, 'alias' => 'Article'
, 'order' => '`Article`.`publish_date` desc'
, 'offset' => $startIndex
, 'joins' => array(
array(
'table' => 'articles_categories',
'alias' => 'ArticlesCategories',
'type' => 'inner',
'conditions' => array(
'ArticlesCategories.article_id = Article.id',
),
)
)
, 'limit' => $this->maxNumArticlesToLoadPerPage
, 'conditions' => "ArticlesCategories.category_id = $categoryId and publish_date <= now()"
, 'fields' => array('Article.id', 'Article.name', 'Article.search_engine_teaser', 'Article.image_file_name', 'Article.content_modified', 'ArticlesCategories.category_id')),
$article
);
$query = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $query);
$articles = $article->query($query);
// also return how many total articles are in this category
$numArticlesInCategory = $article->query('select found_rows()')[0][0]['found_rows()'];
Now, where real curiosity here is that I used ET GLOBAL general_log = 'ON'
in the mysql database to track down what's going on, and it appears that the first query is indeed run several times in a loop, but the query select found_rows()
is only run once!
I also tried to run select SQL_NO_CACHE found_rows()
, but that didn't help.
Has anyone encountered this?