简体   繁体   English

PHP MySqli多个关键字搜索

[英]PHP MySqli Multiple Keyword Search

I use ajax search in my site. 我在自己的网站上使用Ajax搜索。 The user can search through homes. 用户可以搜索房屋。 I want to enable user to do an exact search with exactly the keywords he wants. 我想让用户使用他想要的关键词进行精确搜索。 But the my problem is I don't know from what the user types how many keywords I can search. 但是我的问题是,我不知道从什么用户键入的内容中我可以搜索多少个关键字。 This is my query: 这是我的查询:

$query = "Select a.*,c.name as agent_name,d.country_name,e.state_name,g.city from #__osrs_properties as a"
                                ." inner join #__osrs_agents as c on c.id = a.agent_id"
                                ." inner join #__osrs_types as f on f.id = a.pro_type"
                                ." inner join #__osrs_categories as j on j.id = a.category_id"
                                ." inner join #__osrs_countries as d on d.id = a.country"
                                ." inner join #__osrs_states as e on e.id = a.state"
                                ." inner join #__osrs_cities as g on g.id = a.city"
                                ." where a.approved = '1' and a.published = '1' and a.category_id = '$category->id' and (";
                        //

                        $newKeyword = explode(" ",$keyword);    
                        $query1="j.category_name like '%$newKeyword[0]%' and f.type_name like '%$newKeyword[1]%' and  g.city like '%$newKeyword[2]%'";

it works but when user only types 3 keywords and no more. 它有效,但是当用户仅键入3个关键字且没有其他内容时。

use OR instead of AND 使用OR代替AND

$query1 = "j.category_name like '%$newKeyword[0]%' OR f.type_name like '%$newKeyword[1]%' OR g.city like '%$newKeyword[2]%'";

I hope it will help you. 希望对您有帮助。

You will need to somehow know what the user's are supplying. 您将需要以某种方式知道用户正在提供什么。 Eg what fields you want to match. 例如,您要匹配哪些字段。 Right now, you assume the keywords are in a certain order, but is that guaranteed? 现在,您假设关键字按一定顺序排列,但是可以保证吗? I suspect not if you can't be sure the user will supply all 3. 我不确定是否不能确定用户是否将全部提供3个。

I would suggest breaking up the keywords into fields that can be determined and apply them individually to the query. 我建议将关键字分解为可以确定的字段,并将其分别应用于查询。 Otherwise it will be impossible to know which of the 3 keywords was supplied. 否则,将不可能知道提供了3个关键字中的哪个。

Also you will want to sanitize the user supplied values to prevent malicious injection. 另外,您还需要清理用户提供的值以防止恶意注入。

For getting exact result you should use regular expression in query 为了获得准确的结果,您应该在查询中使用正则表达式

$query1="j.category_name REGEXP '$newKeyword[0]' and f.type_name REGEXP '$newKeyword[1]' and g.city REGEXP '$newKeyword[2]'"; $ query1 =“ j.category_name REGEXP'$ newKeyword [0]'和f.type_name REGEXP'$ newKeyword [1]'和g.city REGEXP'$ newKeyword [2]'”;

The best option is not to try to re-invent the wheel and just use FULLTEXT index in MySQL. 最好的选择是不要尝试重新发明轮子,而只是在MySQL中使用FULLTEXT索引。 Then just prepend all your keywords with + (plus sign) and search by normal SELECT . 然后,只需在所有关键字前面加上+(加号),然后按常规SELECT进行搜索即可。 MySQL will take care of your keywords mix regardless of order and regardless of field size in your database. MySQL会照顾您的关键字组合,而与数据库中的顺序和字段大小无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM