SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help: search with condition (mysql + php)

    hi, just one more question sir please,

    $query = "select * from users where username like \"%$trimmed%\" OR fname like \"%$trimmed%\" AND status='active' AND position='Customer'
    order by username";


    i want to display all the result ONLy if status is active and position is customer but this is not working please help

    Thanks a lot

  2. #2
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use parentheses to group the ORs and make the ANDs separate:

    $query = "select * from users where ( username like \"%$trimmed%\" OR fname like \"%$trimmed%\" ) AND status='active' AND position='Customer'
    order by username";

    I can never remember exactly how the ORs and ANDs get OR'd and AND'ed together without parentheses, so I always use parentheses whenever I have ORs and ANDs in the same query. That approach has never been wrong.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, it's working

    Quote Originally Posted by sonjay View Post
    Use parentheses to group the ORs and make the ANDs separate:

    $query = "select * from users where ( username like \"%$trimmed%\" OR fname like \"%$trimmed%\" ) AND status='active' AND position='Customer'
    order by username";

    I can never remember exactly how the ORs and ANDs get OR'd and AND'ed together without parentheses, so I always use parentheses whenever I have ORs and ANDs in the same query. That approach has never been wrong.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •