Jump to content

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'";

 

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.