PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

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

 February 15, 2022     cakephp-3.0, date, mysql     No comments   

Issue

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.

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

Solution

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"));
    })
    ->contain(['Users'])
    ->order(['Bookings.id' => 'DESC'])
    ->limit(5);


Answered By - arilia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing