CodeIgniter ActiveRecord一次一行

时间:2013-01-24 11:20:16

标签: php mysql codeigniter activerecord

我正在运行一个选择多行的查询。当我遍历结果时,我想一次获取一行(因为它的内存太多而无法一次获取所有内容)。

问题是,CodeIgniter的所有变量的Active Record $ query->行将从数据库中获取所有记录到内存中(并返回单行)。我查看了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;
}

这会将数据库中的所有记录提取到内存中。

是否有一种解决方法可以让我在不将整个结果集加载到内存中的情况下获取单行?

1 个答案:

答案 0 :(得分:2)

<强>解

$query = $this->db->get();
while($r = $query->_fetch_object()){
    ...
}

可能是他们把它公之于众的原因......

相关问题