搜索功能使用框架codeigniter,php

my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.

my search controller function

  public function search()
  {
    $li = $this->session->userdata('logged_in');
    $id = $this->session->userdata('idnumber');
    if($li == TRUE)
    {
      $this->load->model('users_model');
      $this->load->helper('smiley');
      $this->load->library('table');
      $image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
      $col_array = $this->table->make_columns($image_array, 8);
      $image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
      $col_array2 = $this->table->make_columns($image_array2, 8);
      $this->data['smiley_table1'] = $this->table->generate($col_array);
      $this->data['smiley_tables'] = $this->table->generate($col_array2);           
      if($this->input->post())
      {
        $search = $this->input->post('search');
        $memb = $this->users_model->search($search);
        $usersearch =  $this->users_model->search($search);
        $grpsearch =  $this->users_model->searchgrp($search);
        redirect ('home/profile/'.$memb);
      }
    }
 }

My users_model model

 public function search($search)
 {
   $this->db->select('*');
   $this->db->from('users');
   $this->db->like('username',$search);
   $this->db->or_like('fname',$search);
   $this->db->or_like('lname',$search);
   $this->db->or_like('mname',$search);
   $query = $this->db->get();
   foreach ($query->result_array() as $row)
   {
      $memb = $row['idnumber'];
   }
   $error = 'There is no record for the one you searched. Please go Back.';
   $query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
   $hehe = $query1->result_array();
   if($hehe==NULL)
   {
     echo $error; exit;  
   }
   else 
   {
    return $memb;
   }
 }

Always check the varibles using in condition are declared before the condition. If it doesnt pass the condition what would be the output

        public function search($search)
         {
           $this->db->select('*');
           $this->db->from('users');
           $this->db->like('username',$search);
           $this->db->or_like('fname',$search);
           $this->db->or_like('lname',$search);
           $this->db->or_like('mname',$search);
           $query = $this->db->get();
           $memb = null;
           foreach ($query->result_array() as $row)
           {
              $memb = $row['idnumber'];
           }
           $error = 'There is no record for the one you searched. Please go Back.';
           $query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
           $hehe = $query1->result_array();
           if($hehe==NULL)
           {
             return $error;
           }
           else 
           {
            return $memb;
           }
         }

Make sure the variables that youll be using in queries are set. Trap before executing queries. Also, use Query Builder (Active Record) when you can.

public function search($search)
{
   $this->db->select('*');
   $this->db->from('users');
   $this->db->like('username',$search);
   $this->db->or_like('fname',$search);
   $this->db->or_like('lname',$search);
   $this->db->or_like('mname',$search);
   $query = $this->db->get();

   $memb = '';
   if ($query->result_array() > 0) {
      foreach ($query->result_array() as $row) {
         $memb = $row['idnumber'];        
      }
      if ($memb) {

         $this->db->select("*");
         $this->db->where("idnumber", $memb);
         $query1 = $this->db->get("users");

         //$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");

         $hehe = $query1->result_array();
         return ($hehe) ? $memb : false;
      }
      return false;
   } 

  $error = 'There is no record for the one you searched. Please go Back.';
  return $error;
}