Jump to content

AND and OR in query


wkilc

Recommended Posts

Hi all,

 

I am placing First and Last names together as $name; and Phone and E-mails together, as $contact.

 

 $query =
       "SELECT user.Member,user.First,user.Last,user.Phone,user.Email,user.Type
        FROM user
        WHERE ((user.MemberNo LIKE '$MemberNo%')
      and (user.First LIKE '%$name%')
      or (user.Last LIKE '%$name%')
      and (user.Phone LIKE '$contact%')
      and (user.Email LIKE '$contact%')
      and (user.Type LIKE '$Type%'))
     ORDER BY user_stuff.$sort LIMIT $eu, $limit ";
$result = mysql_query($query);

 

I use the OR becasue I want to be able to search the "whole" name, first and last.  When I run a URL query, it works for name, but  everything else (Type, contact, Member)won't filter by URL.  If I remove the OR and replace it with AND, everything else works again.

 

Is this something that happens after using OR?  (Except for name)

 

Help please?  Do I need to separate the query?

 

~Wayne

Link to comment
Share on other sites

$query =
       "SELECT user.Member,user.First,user.Last,user.Phone,user.Email,user.Type
        FROM user
        WHERE ((user.MemberNo LIKE '$MemberNo%')
      and ((user.First LIKE '%$name%')
      or (user.Last LIKE '%$name%'))
      and (user.Phone LIKE '$contact%')
      and (user.Email LIKE '$contact%')
      and (user.Type LIKE '$Type%'))
     ORDER BY user_stuff.$sort LIMIT $eu, $limit ";

 

You need to bracket your OR statement together. Right now it is being evaluated as (MemberNo and First) OR (last and phone and email and type)

Link to comment
Share on other sites

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.