[英]Creating advanced search query with PHP Mysqli
I've started developing a project and I need use a advanced search query with PHP and Mysqli , but I'm getting some troubles. 我已经开始开发项目,并且需要在PHP和Mysqli中使用高级搜索查询,但遇到了一些麻烦。 Check out below my query.
在我的查询下面查看。
$query = "SELECT id, name, keybox, posterid, alias, xmdb from `datasys` WHERE name LIKE '%".$filter."%' OR keybox LIKE '%".$filter."%' ORDER BY id LIMIT 0, 10";
It's working perfeclty! 工作完美! But still not matching entries that contains symbols, for example, if my database contains a row called "Color: red" and someone type "Color red" , the query will return empty, but if I type "Color: red" returns found.
但是仍然不匹配包含符号的条目,例如,如果我的数据库包含名为“ Color:red”的行,并且键入“ Color red” ,则查询将返回空,但是如果我键入“ Color:red”,则返回找到。 So, what should I do to resolve this issue?
那么,我该怎么做才能解决此问题?
Thanks 谢谢
The one thing that I can think of in order to maintain consistency is that you can eliminate the ':' in your query for matching. 为了保持一致性,我可以想到的一件事是,您可以在查询中消除匹配的“:”。 Try the below query:
请尝试以下查询:
$query = "SELECT id, name, keybox, posterid, alias, xmdb from `datasys` WHERE REPLACE(name, ':', '') LIKE '%REPLACE(".$filter.", ':', '')%' OR REPLACE(keybox, ':', '') LIKE '%REPLACE(".$filter.", ':', '')%' ORDER BY id LIMIT 0, 10";
Hope this helps. 希望这可以帮助。
Either use the REGEXP as:- 可以将REGEXP用作:-
SELECT * FROM table WHERE columnName REGEXP "regular_expression";
or use REPLACE() function in WHERE clause as:- 或在WHERE子句中将REPLACE()函数用作:-
SELECT * FROM table WHERE REPLACE(columnName, ":", "") LIKE "like_expression";
Use two queries to find exact and similar records. 使用两个查询来查找准确和相似的记录。 If the records available in Exact data query then no need to run the similar data query
如果精确数据查询中的记录可用,则无需运行类似的数据查询
<?php
//Fetches Exact Data
$query = "SELECT id, name, keybox, posterid, alias, xmdb from `datasys` WHERE name LIKE '%".$filter."%' OR keybox LIKE '%".$filter."%' ORDER BY id LIMIT 0, 10";
//If the previous query doesn't return any value, then use
//Split the the user input and then query DB
$condition="name!=''";
$filters=explode(" ",$filter);
foreach($filters as $val)
{
$condition.=" or (name LIKE '%$val%' OR keybox LIKE '%$val%')";
}
//Fetches Similar Data
$query = "SELECT id, name, keybox, posterid, alias, xmdb from `datasys` WHERE $condition ORDER BY id LIMIT 0, 10";
//-----------ENDS
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.