CodeIgniter Active Record: Load One Row at a Time

后端 未结 4 636
迷失自我
迷失自我 2020-12-19 15:33

The normal result() method described in the documentation appears to load all records immediately. My application needs to load about 30,000 rows, and one at a

4条回答
  •  爱一瞬间的悲伤
    2020-12-19 16:13

    Well, there'se the row() method, which returns just one row as an object, or the row_array() method, which does the same but returns an array (of course).

    So you could do something like

    $sql = "SELECT * FROM yourtable";
    $resultSet = $this->db->query($sql);
    $total = $resultSet->num_rows();
    
    for($i=0;$i<$total;$i++) {
      $row = $resultSet->row_array($i);
    }
    

    This fetches in a loop each row from the whole result set.
    Which is about the same as fetching everyting and looping over the $this->db->query($sql)->result() method calls I believe.

    If you want a row at a time either you make 30.000 calls, or you select all the results and fetch them one at a time or you fetch all and walk over the array. I can't see any way out now.

提交回复
热议问题