CodeIgniter Forums
Using count_all_results or get_compiled_select with $this->db->get() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Using count_all_results or get_compiled_select with $this->db->get() (/showthread.php?tid=81597)



Using count_all_results or get_compiled_select with $this->db->get() - xanabobana - 03-23-2022

How do I use get_compiled_select or count_all_results before running the query without getting the table name added twice? When I use $this->db->get('tblProgram') after either of those, I get the error:
Code:
Not unique table/alias: 'tblProgram'

SELECT * FROM (`tblProgram`, `tblProgram`) JOIN `tblPlots` ON `tblPlots`.`programID`=`tblProgram`.`pkProgramID` JOIN `tblTrees` ON `tblTrees`.`treePlotID`=`tblPlots`.`id` ORDER BY `tblTrees`.`id` ASC LIMIT 2000

Here is my code:
Code:
[align=left][code]
public function get_download_tree_data($options=array(), $rand=""){

//join tables and order by tree id
  $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');

//get number of results to return
  $allResults=$this->db->count_all_results('tblProgram', false);

//chunk data and write to CSV to avoid reaching memory limit
  $offset=0;
  $chunk=2000;
  $treePath=$this->config->item('temp_path')."$rand/trees.csv";
  $tree_handle=fopen($treePath,'a');
  while (($offset<$allResults)) {
      $this->db->limit($chunk, $offset); 
      $result=$this->db->get('tblProgram')->result_array();
      foreach ($result as $row) {
          fputcsv($tree_handle, $row);
      }   
      $offset=$offset+$chunk;
    }
                   
    fclose($tree_handle);
    return array('resultCount'=>$allResults);
[/align]

[/code]


RE: Using count_all_results or get_compiled_select with $this->db->get() - ignitedcms - 03-29-2022

If you have two duplicate column names in two or more different tables and you're doing a 'join' you have to specify an alias I believe.