php - CodeIgniter ActiveRecord 一次一行

标签 php mysql codeigniter activerecord

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

问题是,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/

相关文章:

php - 自动对列进行排序

mysql - 从一个值中减去多个值

mysql - 如何根据当前登录的用户输入数据?

php - 在MySQL中是存储冗余信息还是在必要时连接表更好?

php - 帮助我理解 CURLOPT_READFUNCTION

mysql - 使用 MariaDB 的 Ruby convert_tz 返回 nil

php - 无法使用 AJAX 将数组数据从 View 传递到 Controller

远程服务器上的 CodeIgniter 默认页面

php - DOMDocument 获取元素属性

php - Laravel 5.2 中无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]