Caching mysql results in cache table to be retrieved later 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-&gt;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 ?
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-&gt;data;

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