galchen
galchen

Reputation: 5290

CodeIgniter ActiveRecord one row at a time

I'm running a query that selects many rows. When I loop over the results, I want to fetch a single row at a time (since it's too much memory to fetch them all at once).

Problem is, CodeIgniter's Active Record $query->row for all it's variations will fetch all the records from the database into the memory (and return a single row). I looked into the source of DB_result.php and found:

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];
}

and inside result_object():

...
while ($row = $this->_fetch_assoc())
{
    $this->result_array[] = $row;
}

This fetches ALL of the records from the database into the memory.

Is there a workaround that will allow me to fetch a single row without loading the entire resultset into the memory?

Upvotes: 0

Views: 831

Answers (1)

galchen
galchen

Reputation: 5290

SOLUTION

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

probably the reason they left it public...

Upvotes: 2

Related Questions