2016-04-29 48 views
1

當會話StaffID匹配reports表中的StaffID時,我試圖從report檢索所有報告。會話ID匹配表時獲取數據

Report_Name, ReportDate and ReportID are part of the report table 

這是我覺得應該去

report其中ReportID獲取ReportIDReport_NameReportDateRead_Report比賽ReportIDreport 當會話StaffID = StaffIDRead_Report

和th是我的查詢

function get_read_report() 
    { 
     $this->db->select('report.Report_Name, report.ReportDate, report.ReportID') 
      ->from('Read_Report') 
      ->join('Read_Report', 'report.ReportID = Read_Report.ReportID') 
      ->where('StaffID', $this->session->userdata("StaffID")); 
     return $result = $this->db->get(); 
    } 

我得到這個錯誤

錯誤編號:1066

Not unique table/alias: 'Read_Report' 

SELECT `report`.`Report_Name`, `report`.`ReportDate`, `report`.`ReportID` FROM `Read_Report` JOIN `Read_Report` ON 

reportReportID = Read_ReportReportID WHERE StaffID = '3'

Filename: models/report/Report_model.php 

Line Number: 91 

控制器代碼

function my_read_reports() 
    { 
     $data = array(); 

     if ($query = $this->report_model->get_read_report()) { 
      $data['reports'] = $query; 
     } 

     $this->template['middle'] = $this->load->view($this->middle = 'pages/read_reports_view', $data, true); 
     $this->layout(); 
    } 
+0

你只是錯過了正確的表在加盟的時候。 –

回答

3

您需要report表不一樣的表

更改加入你的表

->join('Read_Report', 'report.ReportID = Read_Report.ReportID') 

TO

->join('report', 'report.ReportID = Read_Report.ReportID') 

您需要查詢更改爲

$this->db->select('report.Report_Name, report.ReportDate, report.ReportID') 
      ->from('report') 
      ->join('Read_Report', 'report.ReportID = Read_Report.ReportID') 
      ->where('Read_Report.StaffID', $this->session->userdata("StaffID")); 
     $result = $this->db->get(); 
     return $result->result();// fetch data then return 
+0

嗯獲取未定義的屬性:mysqli :: $ ReportDate – Beep

+0

'ReportDate'屬於哪個表? – Saty

+0

報告表,與Report_Name和ReportID相同 – Beep

2

您的查詢應該是

$this->db->select('report.Report_Name, report.ReportDate, report.ReportID') 
     ->from('Read_Report') 
     ->join('report', 'report.ReportID = Read_Report.ReportID') 
     ->where('StaffID', $this->session->userdata("StaffID")); 
     return $result = $this->db->get();