使用此获取月份和年份->;db->;代码点火器上的位置


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;
    }
}   

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

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

首先您的控制器

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;
    }
}