Reputation: 380
Hello i am facing a problemm using the query builder in CodeIgniter. The problem is that it wraps the date using single quotes. The query is shown like this:
SELECT *
FROM `enquiries`
WHERE DAY(enquiry_date) = 'DAY(CURDATE())'
AND YEAR(enquiry_date) = 'YEAR(CURDATE())'
AND MONTH(enquiry_date) = 'MONTH(CURDATE())'
of course it will never return any results because of the single quotes. Is there any way i get rid of them? or do i have to do a hardcoded query?
If i execute the same query from phpmyadmin sql console like this
SELECT * FROM `enquiries` WHERE DAY(enquiry_date) = DAY(CURDATE()) AND MONTH(enquiry_date) = MONTH(CURDATE()) AND YEAR(enquiry_date) = YEAR(CURDATE())
it works fine.
My method:
public function enquiries_get(){
$this->load->model('Model_enquiries');
$enquiries = $this->Model_enquiries->get_many_by(array('DAY(enquiry_date)' => 'DAY(CURDATE())', 'YEAR(enquiry_date)' => 'YEAR(CURDATE())', 'MONTH(enquiry_date)' => 'MONTH(CURDATE())' ));
if ($enquiries) {
$this->response(array('status' => 'success', 'status_code' => '200', 'message' => '200 OK', 'response' => $enquiries));
} else {
$this->response(array('status' => 'failure', 'status_code' => '404', 'message' => '404 Not Found', 'response' => 'We couldn\'t find any enquiries for today. Please check again later.'), REST_Controller::HTTP_NOT_FOUND);
}
}
any ideas?
Upvotes: 2
Views: 164
Reputation: 604
you can try php function to get today's date ,year and month like this....
$year = date('Y');
$month = date('m');
$day = date('D');
And pass variables in your query directly...!!
Upvotes: 1