使用 this->db->where on codeigniter 获取月份和年份

Get month and year with this->db->where on codeigniter

我的项目仍然存在问题。我自己以前的帖子,但没有答案。我想调用mysql数据库的结果数据POST.

的年月

这是我的控制器

public function report(){
$nomor = $this->input->post('nomor_reseller');
$bulan = $this->input->post('month'); // YYYY-MM

$report_data = $this->hasil_m->get_data($nomor, $bulan );
}

要是打电话给她上班的月份就好了,如果我输入年月都不行

这是我的模型

function get_data($nomor,$bulan) {
    $this->db->select('*');
    $this->db->from('tb_pelanggan as pel');
    $this->db->join('tb_produk as pro', 'pel.id_barcode  = pro.id_barcode');    
    $this->db->select_sum('jumlah');
    $this->db->where('MONTH(pel.tgl_pembelian)',$bulan);
    $this->db->group_by('pel.id_barcode');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
        } else {
        return false;
    }
}   

首先是你的控制器

public function report(){
$nomor = $this->input->post('nomor_reseller');
$bulan = $this->input->post('month'); // MM
$tahun = $this->input->post('year'); // YYYY

$report_data = $this->hasil_m->get_data($nomor, $bulan, $tahun );
}

和你的模特

function get_data($nomor,$bulan,$tahun) {
    $this->db->select('*');
    $this->db->from('tb_pelanggan as pel');
    $this->db->join('tb_produk as pro', 'pel.id_barcode  = pro.id_barcode');    
    $this->db->select_sum('jumlah');
    $this->db->where('MONTH(pel.tgl_pembelian)',$bulan);
    $this->db->where('YEAR(pel.tgl_pembelian)',$tahun);
    $this->db->group_by('pel.id_barcode');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
        } else {
        return false;
    }
}   

为什么不直接使用 DATE_FORMAT 而不是 YEAR 和 MONTH

->where("DATE_FORMAT(column_name,'%Y-%m')", $bulan)