0
    $all = $this->input->get('all');

    if($all)
    {
        $keywords = $this->input->get('search');
        $data['search'] = $keywords;
        $this->session->set_flashdata('search', $keywords);
        $query = "SELECT *  FROM `investOffers`, `news`";
        $counts = "SELECT count(*) as count FROM  `investOffers`, `news`";
        $first = false;
        if($keywords)
        {
            $data['keywords'] = $keywords;
            $this->session->set_flashdata('keywords', $keywords);
            $keywords = explode(" ", $keywords);
            foreach($keywords as $k)
            {
                if(!$first)
                {
                    $query .= " WHERE investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                    $query .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                    $counts .= " WHERE investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                    $counts .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                    $first = true;

                }
                else
                {
                    $query .= " OR investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                    $query .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                    $counts .= " OR investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                    $counts .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                }
            }


        }
            $page = $this->uri->segment(2);
            if($page)
            {
                $query .= " ORDER BY investOffers.date DESC LIMIT ".$page.", 10";
                $counts .= " ORDER BY investOffers.date DESC LIMIT 10";
            }
            else
            {
                $query .= " ORDER BY investOffers.date DESC LIMIT 10";
                $counts .= " ORDER BY investOffers.date DESC LIMIT 10";
            }
        $data['query'] = $this->db->query($query);
        $counts = $this->db->query($counts);
        foreach($counts->result() as $q)
        {

        $count = $q->count;
        break;

        }
        $config['base_url'] = base_url().'/search';
        $config['prev_link'] = false;
        $config['next_link'] = false;
        $config['last_link'] = false;
        $config['first_link'] = false;
        $config['suffix'] = '?'.http_build_query($_GET, '', "&");
        $config['cur_tag_open'] = '<strong><img src="'.IMAGE.'pagerArrow.png" class="pagerArrow" />';
        $config['cur_tag_close'] = '</strong>';
        $from = intval($this->uri->segment(2));
        $config['per_page'] =  10;   
        $config['num_links'] = 5;    
        $config['uri_segment'] = 2;  
        $config['total_rows'] = $count; 
        $this->pagination->initialize($config);
        $data['pager'] = $this->pagination->create_links();
        $data['content'] = $this->load->view(SITE.'search', $data, true);
        $this->load->view(SITE.'layout', $data);
        return true;

    }

所以这是一个搜索查询,首先我们检查一个搜索是否是“全部”,这意味着搜索所有站点,所以我制作了一个由空格分隔的关键字数组,输入到表单中,然后我把它放在一个视图中 preg_match 的 flash 会话,这是用于 preg_match 在生成的结果视图中突出显示文本,所以我将查询连接起来添加更多 LIKE ,所以问题是当我输入 1 个关键字时,它给了我不同的结果还可以,但是当我输入 2 个或多个关键字时,它给了我相同的结果,我的意思是一个完全相同的结果列表,我不明白为什么会这样,在视图中一切正常,问题在这个代码位置,我试图在查询中设置 th DISTINCT 关键字,但它没有帮助......

ps:这是codeigniter,MVC,这是一个控制器,我不使用模型,因为我不想打开很多文件:)

4

3 回答 3

1

检查您的

$keywords = explode(" ", $keywords);

返回的长度大于 1。由于您从 GET 获取 $keywords,因此您可能需要先对变量进行 urldecode,然后再将其传递给 explode 函数。

$keywords = urldecode($keywords);
于 2012-05-25T22:33:52.733 回答
0

首先,我认为你应该使用CI Active Record。它做了很多转义来防止像SQL Injection这样的事情。我不是安全专家,但我认为您的代码很容易受到攻击。此外,CI Active Record 还具有Active Record 缓存,这在这种情况下很有用,因为您使用相同的 WHERE 事物进行选择和计数。

要调试,您可以查看最后一个查询echo $this->db->last_query();

在您的代码中,我注意到您在关键字之前和之后留下了空格"LIKE '% {$keyword} %'"。那是你的意图吗?我认为您的代码会错过关键字位于主题开头或结尾的情况。我认为这可能是您正在寻找的原因。

于 2012-05-25T22:43:55.587 回答
0

您的查询完全错误。如果你使用

SELECT * FROM table1, table2

您在这两个表之间进行交叉连接,结果不正确。如果您想从多个具有相似结构的表中获取结果,则应改用 UNION 状态:

SELECT field1, field2, field3 FROM table1
UNION
SELECT field1, field2, field3 FROM table2

如果您想使用 WHERE 子句,您可以通过 2 种方式进行:1:

SELECT field1, field2, field3 FROM (
SELECT field1, field2, field3 FROM table1
UNION
SELECT field1, field2, field3 FROM table2) as tt
WHERE your where conditions

在这种情况下,您可以独立于表名对结果进行排序和限制

2:

SELECT field1, field2, field3 FROM table1
WHERE your where conditions
UNION
SELECT field1, field2, field3 FROM table2
WHERE your where conditions

这样你就会得到正确的结果。有关 UNION 状态的更多信息,请访问http://dev.mysql.com/doc/refman/5.0/en/union.html

于 2012-05-25T23:20:12.273 回答