我試圖通過從資料庫中獲取大量資料并將其寫入 CSV 來獲取大量資料。出于某種原因,下面的代碼僅將第一個塊(2000 行)寫入 CSV。我將 $chunk 和 $limit 變數寫入文本檔案,這些變數正在回圈并寫出正確的值。那么為什么不是 $result=$this->db->get('tblProgram', $chunk, $offset)->result_array(); 抓住下一塊?
你能不運行 $this->db->get('tblProgram', $chunk, $offset)->result_array(); 多次使用不同的偏移量?我還能如何遍歷結果?
我可以確認我從查詢中回傳了超過 200k 行,并且如果我將塊設定為不同的東西,我仍然只會回傳第一個塊。
//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);
}
uj5u.com熱心網友回復:
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 的情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448899.html
