类似Codeigniter的查询,显示不相关的记录

kt06eoxx  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(111)

我是新的Codeigniter。我试图搜索数据库中的记录与类似的查询。我想所有的结果与日期02-2023,但它也显示结果与03-2023任何帮助
这是我的模特课。

public function get_filter($limit, $start, $newvar) {
    $this->db->limit($limit, $start);
    $this->db->like('date', $newvar);
    $query = $this->db->get('mytable');    
    return $query->result_array();
    }

public function filter_count($newvar) {
    $this->db->like('date', $newvar);
    $this->db->from("mytable");
    $count = $this->db->count_all_results();
    return $count;  
    }

我已经尝试了上面部分给出的代码。如果需要更多的细节,请告诉我。我希望得到所有日期为02-2023的记录,但“喜欢”短语也包括03-2023的记录。我总是尝试在发布到论坛之前搜索答案。但不幸的是,我在以前的帖子中找不到它的解决方案。
我还注意到,如果我手动传递“02-2023”,那么它工作得相当好,但不能与$newvar一起工作,而$newvar的值是02-2023(已确认)。

tkqqtvp1

tkqqtvp11#

案例:1尝试这个$newvar日期值类似于“2023-03-03”。

$date = date('Y-m-d', strtotime($newvar));
$this->db->select('*');
$this->db->from('mytable');
$this->db->where('DATE(date)', $date);
$query = $this->db->get();
$res= $query->result();

案例:2我假设您在数据库中存储的日期类似于'Y-m-d H:i:s'。

public function get_filter($limit, $start, $newvar) {

    $startDate = $newvar.' 00:00:00';
    $endDate = $newvar.' 23:59:59';

    $this->db->limit($limit, $start);
    $this->db->like('date', $newvar);
    $this->db->where('date >=', $startDate);
    $this->db->where('date <=', $endDate);
    $query = $this->db->get('mytable');    
    return $query->result_array();
 }


public function filter_count($newvar) {

    $startDate = $newvar.' 00:00:00';
    $endDate = $newvar.' 23:59:59';    

    $this->db->where('date >=', $startDate);
    $this->db->where('date <=', $endDate);
    $this->db->from("mytable");
    $count = $this->db->count_all_results();
    return $count;  
}

相关问题