Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    PHP / MySQL search error

    Hi All,

    I am having some real difficulties with some PHP/MySQL search code. I am using this to display the results of my search:

    Code:
    	$sql = "SELECT * FROM `PCNET`
              WHERE type = 'Directory' AND title REGEXP '$search' OR description REGEXP '$search' OR category REGEXP '$search'
              ORDER BY title ASC";
    The aim is to display search results where the search term is $search (where the term is found in 'title', 'description' or 'category', but also to only show those results that have a type called 'Directory'. However, it is returning lots of results that do not have a type of 'Directory'. Can any give some pointers as to what I am doing wrong here?

    Many thanks,
    Neil

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 207 Times in 204 Posts
    You need to group your OR conditions so they will be combined with your AND condition. I added parenthesis around your OR conditions.

    PHP Code:
    $sql "SELECT * FROM `PCNET`
              WHERE type = 'Directory' AND 
              (title REGEXP '$search' 
               OR description REGEXP '$search' 
               OR category REGEXP '$search')
              ORDER BY title ASC"

    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

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