Codeigniter循环到块查询只得到第一个行块?

sy5wg1nm  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(141)

我尝试从数据库中获取大量数据,并将其写入CSV。(2000行)到CSV。我将$chunk和$limit变量写入一个文本文件,它们将通过循环并写出正确的值。那么为什么$result=$this-〉db-〉get结果数组();抢下一大块呢
你可以不运行$this-〉db-〉get('tblProgram',$chunk,$offset)-〉result_array();用不同的偏移量多次循环?否则我怎么循环结果呢?
我可以确认从查询返回的行数超过200 k,并且如果将chunk设置为其他值,我仍然只能获得返回的第一个块。

//Get rows from tblTrees based on criteria set in options array in downloadable format
    public function get_download_tree_data($options=array(), $rand=""){

        $this->db->reset_query();
        $this->db->join('tblPlots','tblPlots.programID=tblProgram.pkProgramID');
        $this->db->join('tblTrees','tblTrees.treePlotID=tblPlots.id');
        $this->db->order_by('tblTrees.id', 'ASC');

       // $allResults=$this->db->count_all_results('tblProgram', false);
        $allResults=200000;
        $offset=0;
        $chunk=2000;
        $treePath=$this->config->item('temp_path')."$rand/trees.csv";
        $tree_handle=fopen($treePath,'a');
        $tempPath=$this->config->item('temp_path')."$rand/trees.txt";
        $temp_handle=fopen($tempPath,'a');
        
        while (($offset<$allResults)) {
            $temptxt=$chunk." ".$offset."\n";
            fwrite($temp_handle,$temptxt);

            $result=$this->db->get('tblProgram', $chunk, $offset)->result_array();
            foreach ($result as $row) {
                fputcsv($tree_handle, $row);
            }    
            $offset=$offset+$chunk;
        }
                    
            fclose($tree_handle);
            fclose($temp_handle);
            return array('resultCount'=>$allResults);
 
    }
ecbunoof

ecbunoof1#

https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_query_builder.php
看起来调用get方法会重置您的模型:

public function get($table = '', $limit = NULL, $offset = NULL)
    {
        if ($table !== '')
        {
            $this->_track_aliases($table);
            $this->from($table);
        }

        if ( ! empty($limit))
        {
            $this->limit($limit, $offset);
        }

        $result = $this->query($this->_compile_select());
        $this->_reset_select();
        return $result;
    }

我想这是任何版本的ci的情况。

相关问题