CodeIgniter Active Record: Load One Row at a Time

后端 未结 4 632
迷失自我
迷失自我 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:28

    Well, the thing is that result() gives away the entire reply of the query. row() simply fetches the first case and dumps the rest. However the query can still fetched 30 000 rows regardles of which function you use.

    One design that would fit your cause would be:

    $offset = (int)@$_GET['offset'];
    
    $query = $this-db->query("SELECT * FROM table LIMIT ?, 1", array($offset));
    $row = $query->row();
    
    if ($row) {
    
        /* Run api with values */
    
        redirect(current_url().'?offset'.($offset + 1));
    
    }
    

    This would take one row, send it to api, update the page and use the next row. It will alos prevent the page from having a timeout. However it would most likely take a while with 30 000 records and refreshes, so you may wanna adjust your LIMIT ?, 1 to a higher number than 1 and go result() and foreach() multiple apis per pageload.

提交回复
热议问题