0
我想緩存我的mysql結果以減少數據庫負載。 緩存結果我使用下面的函數:緩存並遍歷一個關聯數組數組
$cached = $this->getMemCacheX($name);
if ($cached === false)
{
$res = mysql_query($query, $con) or die(mysql_error());
if ($res === false) exit('Database failure.');
if ($fetch === "assoc")
{
$data = mysql_fetch_assoc($res);
}
else if ($fetch === "array")
{
$data = mysql_fetch_array($res);
}
if (mysql_num_rows($res) === 0) $data = 0;
$cr = $this->saveMemCacheX($name, $data);
if ($cr === false) exit('Cache failure.');
return $data;
}
else
{
return $cached;
}
我的問題是,我需要保存和載入設定過至極我用用,同時要迭代的結果(mysql_fetch_assoc()) - 循環,如:
$res = mysql_query("...");
while ($data = mysql_fetch_assoc($res))
{
...
}
現在我需要將結果存儲在緩存中,但因爲無法存儲$ res,所以我需要首先獲取結果。
如何迭代已獲取的結果集?我嘗試了一些foreach循環,但沒有找到解決方案。
我應該如何獲取結果以便從數據庫結果中獲得關聯數組的數組?