Jump to content

Recommended Posts

Hello,

For some reason I am just not getting the right answer when I do this query and not quite sure why? 

$runinposition = mysql_query("SELECT * FROM bb_off WHERE user='$user' AND test1='test1' OR test2='test' OR test3='test3' OR test4='test4' OR test5='test5' OR test6='test6' AND Result1='result1'OR Result2='result2' OR Result3='result3'OR Result4='result4'");

$runinpositionresult= mysql_num_rows($runinposition);

 

So, basically what I am trying to do is count how many test there are when the user = the user and one of the results???  :'(

I am sure I am way off on this code!  Any help would be great!

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/178802-solved-need-help-with-a-query/
Share on other sites

AND's take precedence over OR's so you have to add the parenthesis (below).  This query will return all rows where user = $user AND any of the six tests AND any of the four results.

SELECT * FROM bb_off WHERE user='$user' 
AND (test1='test1' OR test2='test' OR test3='test3' OR test4='test4' OR test5='test5' OR test6='test6') 
AND (Result1='result1' OR Result2='result2' OR Result3='result3' OR Result4='result4')

 

 

Without the parenthesis it acts like this:

SELECT * FROM bb_off WHERE (user='$user' AND test1='test1') OR test2='test' OR test3='test3' 
OR test4='test4' OR test5='test5' OR (test6='test6' AND Result1='result1') OR Result2='result2' 
OR Result3='result3'OR Result4='result4'

Which is clearly NOT what you want.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.