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
https://forums.phpfreaks.com/topic/210903-and-and-or-in-query/
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
https://forums.phpfreaks.com/topic/210903-and-and-or-in-query/#findComment-1100029
Share on other sites

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.