...

View Full Version : PHP / MySQL search error



howard-moore
08-20-2012, 02:46 PM
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:


$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

djm0219
08-20-2012, 03:22 PM
You need to group your OR conditions so they will be combined with your AND condition. I added parenthesis around your OR conditions.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum