Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MySqli多关键字搜索_Php_Mysql - Fatal编程技术网

PHP MySqli多关键字搜索

PHP MySqli多关键字搜索,php,mysql,Php,Mysql,我在我的站点中使用ajax搜索。用户可以在家中搜索。我想让用户做一个准确的关键字,他想要的确切搜索。但我的问题是,我不知道用户键入了什么,我可以搜索多少关键字。 这是我的疑问: $query = "Select a.*,c.name as agent_name,d.country_name,e.state_name,g.city from #__osrs_properties as a" ." inner join #__osrs_

我在我的站点中使用ajax搜索。用户可以在家中搜索。我想让用户做一个准确的关键字,他想要的确切搜索。但我的问题是,我不知道用户键入了什么,我可以搜索多少关键字。 这是我的疑问:

$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]%'";
但当用户只键入3个关键字而不键入更多关键字时,它就可以工作。

使用或而不是and

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

我希望它能帮助您。

您需要知道用户提供了什么。例如,您希望匹配哪些字段。现在,假设关键字是按一定顺序排列的,但这能保证吗?如果您不能确定用户是否会提供所有3个,我怀疑不会

我建议将关键字分解为可以确定的字段,并将它们单独应用于查询。否则就不可能知道这3个关键字中的哪一个被提供了


此外,您还需要清理用户提供的值以防止恶意注入。

为了获得准确的结果,您应该在查询中使用正则表达式


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

最好的选择不是试图重新发明轮子,而是在MySQL中使用全文索引。然后只需在所有关键词前加上+(加号),然后按正常选择进行搜索。MySQL将处理您的关键字组合,而不考虑顺序和数据库中的字段大小。

您不应该使用MySQL
而不是
?谢谢我编写的代码。我的问题是我不知道用户类型有多少关键字。也许3或5然后你可以检查爆炸后的计数,然后使用循环你会得到你想要的答案。希望它也能帮助你。