[英]How can I solve this simple search query for php MySQL
I have a MySQL table (table1) with following fields... 我有一个具有以下字段的MySQL表(table1)...
id, title, description, detail, category, status ID,标题,描述,详细信息,类别,状态
What I am trying to do is searching these fields with regular php search query... My query for now is... 我想做的是用常规的php搜索查询来搜索这些字段。
$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) 其中
$q
是我的搜索字符串, $cateid
是我的类别ID,状态是On / Off(有效/无效)
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 现在,如果通过此查询获取结果,则有5个结果,而我有查询字符串= foo,并且它仅存在于一个条目中而不是5 ...而且当我将查询更改为时,它也向我显示了status ='0'的行
$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. 它给了我正确的结果...但是我也想分别将我的查询字符串匹配到description和detail字段。 Please help me out.
请帮帮我。
You need to put parentheses () around all the OR conditions 您需要在所有OR条件周围加上括号()
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.