我正在运行一个选择多行的查询。当我循环结果时,我想一次获取一行(因为内存太多,无法一次获取所有行)。
问题是,CodeIgniter 的 Active Record $query->row 的所有变体都会将所有记录从数据库提取到内存中(并返回一行)。我查看了 DB_result.php 的源代码并发现:
public function row_object($n = 0)
{
$result = $this->result_object();
if (count($result) == 0)
{
return $result;
}
if ($n != $this->current_row AND isset($result[$n]))
{
$this->current_row = $n;
}
return $result[$this->current_row];
}
以及 result_object() 内部:
...
while ($row = $this->_fetch_assoc())
{
$this->result_array[] = $row;
}
这会将数据库中的所有记录提取到内存中。
是否有一种解决方法可以让我获取单行而不将整个结果集加载到内存中?
最佳答案
解决方案
$query = $this->db->get();
while($r = $query->_fetch_object()){
...
}
可能是他们将其公开的原因......
关于php - CodeIgniter ActiveRecord 一次一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14500110/