高级搜索代码不带来相关结果(Codeigniter) [英] Advanced search code does not bring relevant results (Codeigniter)

查看:141
本文介绍了高级搜索代码不带来相关结果(Codeigniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个CodeIgniter代码进行高级搜索。这个高级搜索只有一个列表,供用户选择。但是,我有问题,当搜索,它带来的结果从其他城市没有选择的用户。以下是匹配字词的代码:

  $ db-> or_like(title,$ _POST ['search -术语']); // OR 
$ db-> or_like(content,$ _POST ['search-term']); // OR
$ db-> or_like(name,$ _POST ['search-term']); // OR
$ db-> or_like(keywords,$ _POST ['search-term']); // OR

此处是负责与所选省份匹配的代码的一部分:

  if(isset($ _ POST ['province'])&& $ _POST ['province']!=0)
{
$ db-> where(province,$ _POST ['province']);
if(isset($ _ POST ['cities'])&& $ _POST ['cities']!=0)
{
$ db-&城市,$ _POST ['cities']);
}
} // AND

将数据转换为数组:

  $ search_result = $ db-> get(ads) - > result_array ; 

还值得一提的是,当我改变 or_like $ b>

$ b>

$ b>

$ c>
$ b

  SELECT * FROM(`ads`)WHERE`status` = 2 AND`province` ='5'AND`title` LIKE'%شریف% OR`content` LIKE'%شریف%'OR`name` LIKE'%شریف%'OR`keywords'LIKE'%شریف%'ORDER BY'stars DESC 

这似乎是声音和确定,但结果不是...

解决方案

尝试此查询,然后,

  SELECT * FROM(`ads`)WHERE`status` = 2 AND `province` ='5'AND(
`title` LIKE'%شریف%'
OR`content` LIKE'%شریف%'OR`name` LIKE'%شریف%'OR` LIKE'%شریف%')
ORDER BY'stars` DESC


在CI中,您可以这样尝试:

  $ sql =SELECT * FROM `ads`)WHERE`status` = 2 AND`province` ='5'AND(
`title` LIKE'%شریف%'
OR`content` LIKE'%شریف%'OR`name `LIKE'%شریف%'OR`keywords'LIKE'%شریف%')
ORDER BY'stars` DESC;
$ this-> db-> query($ sql);

这将解决您的问题。



边注:
您的代码称欢迎使用SQL注入。
知道你在做什么。


I have written this CodeIgniter code for an advanced search. this advanced search has only a list of provinced for the uses to select from. But, I have problem, when searching, it brings results from other cities not selected by the user. Here is the code to match for the term:

$db->or_like("title", $_POST['search-term']); // OR 
$db->or_like("content", $_POST['search-term']);   // OR 
$db->or_like("name", $_POST['search-term']);    // OR   
$db->or_like("keywords", $_POST['search-term']);    // OR

And here is part of code responsible for matching with the selected province:

if(isset($_POST['province']) && $_POST['province'] != "0")
        { 
            $db->where("province", $_POST['province']); 
            if(isset($_POST['cities']) && $_POST['cities'] != "0")
            {
            $db->where("city", $_POST['cities']);   
            }
        } // AND    

And here is the code responsible for getting the data into an array:

$search_result = $db->get("ads")->result_array();

It is also worth mentioning that when I change the or_like to like it yields no result.

Here is the generated QUERY:

SELECT * FROM (`ads`) WHERE `status` = 2 AND `province` = '5' AND `title` LIKE '%شریف%' OR `content` LIKE '%شریف%' OR `name` LIKE '%شریف%' OR `keywords` LIKE '%شریف%' ORDER BY `stars` DESC

It seems to be sound and OK, but the results are not...

解决方案

try this query then,

SELECT * FROM (`ads`) WHERE `status` = 2 AND `province` = '5' AND(
 `title` LIKE '%شریف%'
 OR `content` LIKE '%شریف%' OR `name` LIKE '%شریف%' OR `keywords` LIKE '%شریف%') 
ORDER BY `stars` DESC

Update: in CI you can try it like this:

$sql = "SELECT * FROM (`ads`) WHERE `status` = 2 AND `province` = '5' AND(
     `title` LIKE '%شریف%'
     OR `content` LIKE '%شریف%' OR `name` LIKE '%شریف%' OR `keywords` LIKE '%شریف%') 
    ORDER BY `stars` DESC";
$this->db->query($sql);

this would solve your problem.

Side Notes: your code is saying welcome to SQL injection. be aware of what you are doing.

这篇关于高级搜索代码不带来相关结果(Codeigniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆