你好,我正面臨使用CodeIgniter中的查詢生成器problemm。問題是它使用單引號包裝日期。查詢如下所示:擺脫日期中的單引號codeigniter
SELECT *
FROM `enquiries`
WHERE DAY(enquiry_date) = 'DAY(CURDATE())'
AND YEAR(enquiry_date) = 'YEAR(CURDATE())'
AND MONTH(enquiry_date) = 'MONTH(CURDATE())'
當然,由於單引號,它永遠不會返回任何結果。有什麼辦法擺脫他們?或者我必須做一個硬編碼查詢?
如果我執行從phpmyadmin的SQL控制檯相同的查詢這樣
SELECT * FROM `enquiries` WHERE DAY(enquiry_date) = DAY(CURDATE()) AND MONTH(enquiry_date) = MONTH(CURDATE()) AND YEAR(enquiry_date) = YEAR(CURDATE())
它工作正常。
我的方法:
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);
}
}
什麼想法?
'set()'函數帶有第三個參數,可以用來保留這些值,這是調用這樣的函數所必需的。 [查詢構建器文檔](https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data)解釋了這一點。 – tadman