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

i am trying to develop a search function. but i am getting wrong search results.
heres my controller
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
function search_communities($search_query)
    $this->db->where('Community_name', '$search_query');
    $Q = $this->db->get();
    if ($Q->num_rows() > 0) {
        return $Q->result();
       } else {
         return FALSE;
and view
<?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

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

i have changed that line to
$this->db->where('Community_name', $search_query);

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

To enable the profiler, just put

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

its generating the right sql
SELECT * FROM (`community`) WHERE `Community_name` = 'z'

You looking for an exact match? If not you should look at using LIKE.

yes i am looking for exact match

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.

it does not return a result while running in PHPmyadmin. and the Community_name field is not indexed.

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