I have a MySQL table (table1) with following fields...
id, title, description, detail, category, status
What I am trying to do is searching these fields with regular php search query... My query for now is...
$q = mysql_query("SELECT * FROM table1 WHERE title LIKE '%$q%' OR description LIKE '%$q%' OR detail LIKE '%$q%' AND category='$cateid' AND status='1' ORDER BY id DESC LIMIT 10");
Where $q
is my search string, $cateid
is my category id and status is On/Off (Active/Inactive)
Now if get results with this query I got 5 results while I have query string = foo and it only exists in one entry not 5... And also it is showing me the rows with status='0' When I change my query to
$q = mysql_query("SELECT * FROM table1 WHERE title LIKE '%$q%' AND category='$cateid' AND status='1' ORDER BY id DESC LIMIT 10");
It gives me correct results... But I also want to match my query string to description and detail fields respectively. Please help me out.
You need to put parentheses () around all the OR conditions
SELECT * FROM table1
WHERE (title LIKE '%$q%' OR description LIKE '%$q%' OR detail LIKE '%$q%')
AND category='$cateid' AND status='1' ORDER BY id DESC LIMIT 10
I think this is the query you need:
$q = mysql_query("SELECT * FROM table1 WHERE
(title LIKE '%$q%' OR detail LIKE '%$q%')
AND category='$cateid' AND status='1' ORDER BY id DESC LIMIT 10");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.