4 Replies - 1348 Views - Last Post: 15 August 2014 - 05:26 AM

#1 bengie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 12-October 13

What is wrong with this sql query?

Posted 15 August 2014 - 05:09 AM

Hi can someone tell me what is wrong with this sql query, I have no idea :(

SELECT * FROM friends WHERE user_1 = '$emailid' or user_1 = '$email' AND WHERE user_2 = '$emailid' or user_2 = '$email'



Thanks in advance :D

Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with this sql query?

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,525
  • Joined: 12-December 12

Re: What is wrong with this sql query?

Posted 15 August 2014 - 05:12 AM

There can only be one WHERE clause. Remove the second word WHERE, and you might need to introduce brackets to get the conditions working correctly.

Also, your field and choice of variable-names are confusing: user_1 = '$emailid' ? :dontgetit:

This post has been edited by andrewsw: 15 August 2014 - 05:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 bengie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 12-October 13

Re: What is wrong with this sql query?

Posted 15 August 2014 - 05:14 AM

Haha, cool. Thanks for your help Andrew :D
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,926
  • Joined: 16-October 07

Re: What is wrong with this sql query?

Posted 15 August 2014 - 05:22 AM

Note, there is an interesting gotcha to be had here
SELECT *
  FROM friends
  WHERE user_1 = '$emailid' 
    or user_1 = '$email' 
    AND user_2 = '$emailid'
    or user_2 = '$email'



Depending on your SQL engine, if user_1 = '$emailid' is true, the rest will be ignored, because the OR stops the evaluation.

You want parens:
  WHERE (user_1 = '$emailid' or user_1 = '$email')
    AND (user_2 = '$emailid' or user_2 = '$email')



There is a shortcut for this that will make it easier to read and get rid of the parens ( well, those parens):
  WHERE user_1 in ('$emailid', '$email')
    AND user_2 in ('$emailid', '$email')


Was This Post Helpful? 2
  • +
  • -

#5 bengie  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 12-October 13

Re: What is wrong with this sql query?

Posted 15 August 2014 - 05:26 AM

Ahh, thank you. I understand much better now :D

Thanks Hagrid :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1