Yii Framework Forum: I am getting sql violation error while quering this code - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

I am getting sql violation error while quering this code I want to use and where in my sql query

#1 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 23-November 16

Posted 16 April 2018 - 02:05 AM

I am trying to put condition in my sql query.

 'query' => Inventory::find()->
                where(['user_id' =>  Yii::$app->user->identity->id ])->
                andWhere('placed_date'<= date('Y-m-d H:i:s')),
        ]),


But I am getting the following error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
The SQL being executed was: SELECT COUNT(*) FROM `inventory` WHERE (`user_id`=113) AND ()
Error Info: Array
(
    [0] => 42000
    [1] => 1064
    [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
)

0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,556
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 April 2018 - 02:15 AM

Check your andWhere(), you did not put there any condition.
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 23-November 16

Posted 16 April 2018 - 02:48 AM

View PostMaurizio Domba Cerin, on 16 April 2018 - 02:15 AM, said:

Check your andWhere(), you did not put there any condition.


I want to put the condition as less than or equal to the present server time How can I do that?

This post has been edited by arunwebber: 16 April 2018 - 02:56 AM

0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,556
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 April 2018 - 02:57 AM

In your code you did not set any condition, check few andWhere() examples and you will get it ;)
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 23-November 16

Posted 16 April 2018 - 03:08 AM

View PostMaurizio Domba Cerin, on 16 April 2018 - 02:57 AM, said:

&nbsp;In your code you did not set any condition, check few andWhere() examples and you will get it ;)

<div><br></div>


andWhere('publish_on_date <= NOW()');


This solved the issue.
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,556
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 April 2018 - 03:08 AM

But do you understand the difference? Now you did give a condition, before not.
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   arunwebber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 23-November 16

Posted 16 April 2018 - 03:16 AM

View PostMaurizio Domba Cerin, on 16 April 2018 - 03:08 AM, said:

But do you understand the difference? Now you did give a condition, before not.


Yeah, Before yii2 took it as an element of an array
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,556
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 April 2018 - 03:22 AM

View Postarunwebber, on 16 April 2018 - 03:16 AM, said:

Yeah, Before yii2 took it as an element of an array


Exactly, and you were missing the condition as the first parameter to andWhere, before that assignment.
Find more about me.... btw. Do you know your WAN IP?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users