PHP MySqli 多关键词搜索

标签 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_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 个关键字时就可以了。

最佳答案

用 OR 代替 AND

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

希望对您有所帮助。

关于PHP MySqli 多关键词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584839/

上一篇:函数外的php foreach

下一篇:mysql添加主键

相关文章:

mysql - 在 mysql 中,如何对不同表中的 2 列求和并按公共(public)列分组

mysql - 如何在 MySQL 中使用 last_insert_id()?

Php 机器学习库?

php - Paypal 支付专业版和 pci 合规性

php - 为在 PHP 中创建的图像添加边框

java - 为 RESTful web 服务设置 MySQL 和 Apache

mysql - 'dob' 中的未知列 'field list'

PHP 与 mysql SELECT LIMIT

php - Paypal IPN 返回日期时间格式

php - 使用ajax将事件添加到mysql数据库