Jump to content

Using AND and OR in a query


jeff5656

Recommended Posts

I tried to search for the answer on google, but it's hard to seach for "and" and "or".

 

Anyway I want this query:

 

$query = "select * from table where (term1= '$term1' or term2 like '$term2%') and user_id ='$id'";

 

such that ALWAYS limit the search to the user_id = $id, but search for either term1 or term2.

 

Is the above query correct?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/221057-using-and-and-or-in-a-query/
Share on other sites

Hi

 

The query looks fundamentally right and should always limit the search to the user_id but I agree with Maq that the part regarding term1 & term2 looks a bit weird in that you are using equals for one comparison and LIKE for another.  Also, do you mean the LIKE clause to start with $term2 or be found anywhere in it?

 

Maybe the following would work better (depending on what exactly you are using it for:-

 

$query = "select * from table where (term1 LIKE '%$term1%' or term2 like '%$term2%') and user_id ='$id'";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.