2

How can i fetch records from mysql results stored in cache table ?

$cached_res_obj = cache_get("expensive_sql", MY_CACHE_TABLE);
$res            = $cached_res_obj->data;
while ($row = db_fetch_array($res)) {
    print_r($row);
}
die(__FUNCTION__);

I dont get any results. If i run the actual query through drupal I get results. What should be done here ?

1 Answer 1

3

You can't store the query result resource directly in the cache table as it can't be serialised. Instead you should loop through that resource when you first run the expensive query, and cache an array of results, like so:

$q = db_query('expensive sql here');
$data = array();
while ($row = db_fetch_object($q)) {
  $data[] = $row;
}

cache_set('expensive_sql', $data, MY_CACHE_TABLE);

Then when you pick it up at the other end you can just loop through that array:

$cached_res_obj = cache_get("expensive_sql", MY_CACHE_TABLE);
$res            = $cached_res_obj->data;

foreach ($res as $row) {
  print_r($row);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.