为什么我得到相同的结果?

时间:2012-05-25 21:50:55

标签: php mysql codeigniter

    $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个关键字时,它给出了我不同的结果和它的okey,但当我输入2个或多个关键字,它给我相同的结果,我的意思是结果列表完全相同,我不明白为什么是这样,在视图中它一切都好,问题是在这个代码的地方,我试图在查询中设置DISTINCT关键字,但它没有帮助......

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

3 个答案:

答案 0 :(得分:1)

检查你的

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

返回的长度大于1.因为您从GET获取$关键字,您可能需要在将变量传递给爆炸函数之前对其进行urldecode。

$keywords = urldecode($keywords);

答案 1 :(得分:0)

首先,我认为你应该使用CI Active Record。为了防止类似SQL Injection之类的东西,它会进行大量的转义。我不是安全专家,但我认为你的代码容易受到攻击。除此之外,CI Active Record还有Active Record Caching,在这种情况下将会很有用,因为您使用相同的WHERE来进行选择和计数。

要进行调试,您可以按echo $this->db->last_query();

查看上一个查询

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

答案 2 :(得分:0)

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

SELECT * FROM table1, table2

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

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

如果要使用WHERE子句,可以通过以下两种方式执行: 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