摆脱日期codeigniter中的单引号

时间:2016-06-08 17:17:52

标签: php mysql codeigniter

您好我正在使用CodeIgniter中的查询构建器来解决问题。问题是它使用单引号包装日期。查询显示如下:

SELECT *
FROM `enquiries`
WHERE DAY(enquiry_date) = 'DAY(CURDATE())'
AND YEAR(enquiry_date) = 'YEAR(CURDATE())'
AND MONTH(enquiry_date) = 'MONTH(CURDATE())'

当然,由于单引号,它永远不会返回任何结果。有什么方法可以摆脱它们吗?或者我是否必须进行硬编码查询?

如果我从phpmyadmin sql console执行相同的查询,就像这样

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

 }

任何想法?

1 个答案:

答案 0 :(得分:1)

你可以尝试使用php函数来获取今天的日期,年份和月份......

$year = date('Y');
$month = date('m');
$day = date('D');

直接在查询中传递变量...... !!