Ashish Ashish - 1 year ago 110
MySQL Question

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

I am using the following query, but when it is running then getting mysql syntax error. I am using cakephp 3 framework. Error is generating -

>where([DATE('Bookings.created') => date("Y-m-d")])
for this condition, so can you suggest how can avoid this issue.

$queryTodayAppointment = $this->Bookings->find('all')
->select(['Users.fullname', 'Bookings.slot_time'])
->where([DATE('Bookings.created') => date("Y-m-d")])
->order(['' => 'DESC'])

Answer Source

Assuming you actually have to use the DATE function there are many ways to implements this.

simplest one (that's what you were already trying to do but you simply forgot to quote the function)

->where(["DATE(Bookings.created)" => date("Y-m-d")])

But you could also use cake SQL functions (reference)

$query = $this->Bookings->find();
$date = $query->func()->date(['Bookings.created' => 'identifier']);
$query->select(['Users.fullname', 'Bookings.slot_time'])
    ->where(function ($exp, $q) use($date) {
        return $exp->eq($date, date("Y-m-d"));
    ->order(['' => 'DESC'])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download