我的数据库表如下所示
| id | user_name | address | contact | date |
|----|-----------|---------|---------|----------|
| 1 | john | NY | 12345 |2015-4-20 |
| 2 | Mart | NY | 54345 |2015-4-05 |
| 3 | Drew | US | 67340 |2015-3-14 |
我的controller功能是
function orders()
{
$data['orders'] = $this->common_model->get_data_between_15days('tbl_orders',array('status'=>'1'));
$data['title']='Orders';
$data['main_content']='users/orders_view.php';
$this->load->view('admin/includes/template',$data);
}
Run Code Online (Sandbox Code Playgroud)
我的model功能是
public function get_data_between_15days($table, $condition)
{
$result = $this->db->get_where($table, $condition);
if($result)
{
return $result->result_array();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想从数据库获取今天和过去15天之间的记录.我试过这样的
$result = $this->db->query('SELECT * FROM '.$table.' WHERE date BETWEEN DATE_SUB(NOW(), INTERVAL 15 DAY) AND NOW(); AND '.$condition);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我想获得过去15到30天之间的所有记录.我很感谢你的帮助.谢谢.
使用CodeIgniter标准查询
$this->db->select('*');
$this->db->where('date BETWEEN DATE_SUB(NOW(), INTERVAL 15 DAY) AND NOW()');
$this->db->where($conditions);
$result = $this->db->get($table);
Run Code Online (Sandbox Code Playgroud)