您好:我有这样的功能
function query($query)
{
if (in_array($query,$this->queries))
{
return $result = $this->results[$query];
}
$this->queries[] = $query;
$result = mysql_query($query);
if ($result)
{
while($row = mysql_fetch_array($result)
{
$this->results[$query][] = $row;
}
//I need to return $result for later use (so I can loop it in while if i need to do so later)
//but problem is that $result is being cleaned up after fetching
}
}
有人/任何人知道解决方案吗?
最佳答案
您已经在函数中获得了一组结果,因此您不应使用 mysql_fetch_assoc
。相反,只需循环遍历结果(假设有结果):
function query($query)
{
if (in_array($query,$this->queries))
{
return $result = $this->results[$query];
}
$this->queries[] = $query;
$result = mysql_query($query);
if ($result)
{
while($row = mysql_fetch_assoc($result)
{
$this->results[$query][] = $row;
}
return $this->results[$query];
}
}
然后使用:
$result = query("SELECT * FROM test");
if(count($result) > 0)
{
foreach($result as $key=>$value)
{
// Do what you need to with the rows
}
}
关于php - 从我自己的 php 函数缓存 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570143/