Welcome Guest, Not a member yet? Register   Sign In
Using count_all_results or get_compiled_select with $this->db->get()
#1

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]
Reply
#2

(This post was last modified: 03-29-2022, 01:08 AM by ignitedcms.)

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.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB