Welcome Guest, Not a member yet? Register   Sign In
Search function
#1

[eluser]junaids[/eluser]
i am trying to develop a search function. but i am getting wrong search results.
heres my controller
Code:
function search_communities()
  {
          $results = $this->searchmodel->search_communities($this->input->post('search_query'));
        $data['results'] = $results;
        $this->load->view('search/search_result', $data);
  
}
and model
Code:
function search_communities($search_query)
    {
    $this->db->select('*');
    $this->db->from('community');
    $this->db->where('Community_name', '$search_query');
    $Q = $this->db->get();
    if ($Q->num_rows() > 0) {
        return $Q->result();
       } else {
         return FALSE;
    }
}
and view
Code:
<?php if (empty($_POST['search_query'])):?>
<p>No search query submitted.</p>
&lt;?php else:?&gt;
    &lt;?php if (count($results)):?&gt;
<p>&lt;?php echo count($results) ?&gt; result(s) returned for query: &lt;?php echo $_POST['search_query'];?&gt;</p>
there are only two records in community table. but whatever is submit query , it returns a result. i have also tried the $this->db->like statement but it also gives the same result. any suggestions
#2

[eluser]Dam1an[/eluser]
I think the fact you have single quotes round your $variable might be causing a problem, you don't need them, and single quotes mean the literal value (unlike double quotes which parse values)

Also, enable the profiler to see what SQL it generates for you
#3

[eluser]junaids[/eluser]
i have changed that line to
Code:
$this->db->where('Community_name', $search_query);

but the result is same..
how do i enable profiler?
#4

[eluser]Dam1an[/eluser]
To enable the profiler, just put
Code:
$this->output->enable_profiler(TRUE);

in your code (I normally put it in the constrcutor, but I don't think it matter where in the controller you put it)
#5

[eluser]junaids[/eluser]
its generating the right sql
Code:
SELECT * FROM (`community`) WHERE `Community_name` = 'z'
#6

[eluser]garymardell[/eluser]
You looking for an exact match? If not you should look at using LIKE.
#7

[eluser]junaids[/eluser]
yes i am looking for exact match
#8

[eluser]TheFuzzy0ne[/eluser]
What happens when you run the generated query directly against the database via PHPMyAdmin or similar? If 'z' exists in that field, then it should find it. Is your the field indexed? Even if it's not, you should still be able to get a result.
#9

[eluser]junaids[/eluser]
it does not return a result while running in PHPmyadmin. and the Community_name field is not indexed.
#10

[eluser]slowgary[/eluser]
So... is there a row in your table where 'Community_name' is 'z'? It seems like the answer is 'no', because otherwise you'd get a result. Have you figured it out yet?

Also, I recommend all lowercase names for consistency... your table is named 'community', but your field is named 'Community_name', why? Should be 'community_name' or just 'name'.




Theme © iAndrew 2016 - Forum software by © MyBB