目前,我正在嘗試從特定時間范圍內的資料中檢索所有條目。為此,我的模型類中有一個方法,其中包含以下陳述句:
public function get_records_all($st_date,$end_date){
$sql = SELECT
*
FROM `crm_listings`
WHERE added_date BETWEEN '" . $st_date . "' AND '".$end_date."'
ORDER BY `added_date` DESC;
$response = $this->db->query($sql);
echo $response;
}
在我的控制器類中,我使用以下陳述句來顯示輸出:
function fetch_status(){
$startDate = '';
$endDate = '';
$this->load->model('crm/user_model');
if($this->input->post('startDate')){
$startDate = $this->input->post('startDate');
}
if($this->input->post('endDate')){
$endDate = $this->input->post('endDate');
}
$data_all = $this->user_model->get_records_all($startDate,$endDate);
}
但這給了我以下錯誤:

uj5u.com熱心網友回復:
嘗試這個
CodeIgniter 使您可以訪問查詢生成器類。此模式允許使用最少的腳本在資料庫中檢索、插入和更新資訊。在某些情況下,執行資料庫操作只需要一兩行代碼。CodeIgniter 不要求每個資料庫表都是它自己的類檔案。相反,它提供了一個更簡化的界面。
public function get_records_all($st_date,$end_date){
$this->db->where('added_date >=', $st_date);
$this->db->where('added_date <=', $end_date);
$this->db->order_by('added_date', 'DESC');
return $this->get('crm_listings')->result();
}
更多使用這個鏈接CI Query Builder CLass
uj5u.com熱心網友回復:
如果你堅持使用db->query改變你的get_records_all()回應,return $response;那么你可以$data_all像這樣使用as 和 object
foreach($data_all as $data) {
echo $data->some_field;
}
uj5u.com熱心網友回復:
你忘了->result()在$this->db->query($sql)喜歡之后添加:
$response = $this->db->query($sql)->result();
但是,您也可以使用查詢生成器,例如
public function get_records_all($st_date,$end_date){
return $this->db->where('added_date >=', $st_date)
->where('added_date <=', $end_date);
->order_by('added_date', 'DESC');
->get('crm_listings')
->result();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315051.html
