0

I'm drawing a blank this late at night after programming all day and maybe someone else can help shed light on where my issue lies. I'm trying to make a MySQL query in PHP where it gets the correct fields based on multiple variables using AND and OR.

Here is what I'm trying to do that is not working:

$result = mysql_query("SELECT * FROM tableName 
      WHERE key = '$key' ((userFrom = '$userFrom' AND userTo = '$userTo') 
                       OR (userFrom = '$userTo' AND userTo = '$userFrom')");

Basically in English what this does is it needs to know the Key value. Once it matches the key value then I need all the rows with that key value that are ALSO - FROM or TO the same users - think of it as a conversation between two people it has to have their conversation topic (key) and the two participants (from and to).

Any suggestions?

2
  • You're missing an AND between key = '$key' and the following parenthesis – is this a typo in your example? Commented Nov 3, 2011 at 2:58
  • Yeah I accidentally forgot to type that. I do have the AND in there in fact and still not working unfortunately. Commented Nov 3, 2011 at 3:02

2 Answers 2

2

Looks like three open parens and two closing parens to me. The missing one is immediately before the closing double quote. (Plus, of course, the missing AND mentioned by others).

Also, of course, various characters embedded in user names (if allowed) will make the command fail in data-specific ways. For that reason, and to avoid SQL Injection attacks, please switch to parameterized queries.

Sign up to request clarification or add additional context in comments.

1 Comment

You're correct. I re-read this another time, evaluated the parameters being POST to my page and realized there was an issue with it.
0

I think it's just a question of getting the parenthesis the correct way. Try this:

$result = mysql_query("SELECT * FROM tableName 
                       WHERE key = '$key' **AND** 
                       ((userFrom = '$userFrom' AND userTo = '$userTo') OR 
                        (userFrom = '$userTo' AND userTo = '$userFrom')");

Or perhaps:

$result = mysql_query("SELECT * FROM tableName 
                        WHERE ((key = '$key') AND 
                        ((userFrom = '$userFrom' AND userTo = '$userTo') OR 
                         (userFrom = '$userTo' AND userTo = '$userFrom'))");

I think if you fiddle with those you will get what you want.

4 Comments

Didn't seem to do the trick. I've been fiddling around with the positioning of the parenthesis and haven't had much luck strike me quite yet.
I ran a query with a similar structure (with the extra closing parenthesis that Larry mentioned was missing) and it went off without a hitch.
Do you have the exact query? I tried that and didn't work.. Here is my updated query: $result = mysql_query("SELECT * FROM chatloudChats WHERE ((apiKey = '$apiKey') AND ((userFrom = '$userFrom' AND userTo = '$userTo') OR (userFrom = '$userTo' AND userTo = '$userFrom')))");
@Brayden, don't show us the php, show us the constructed SQL statement including the data substitution. Because you are generating dynamic SQL, the contents of the variables matters (see my answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.