2017-05-24 42 views
1

我有這個功能模型笨模型功能和/或查詢

function select($table,$match,$or){ 
     $this->db->select('*'); 
     $this->db->from($table); 
     $this->db->where($match)->or_where($or); 
     $query = $this->db->get(); 
     $query_result = $query->result(); 
     return $query_result; 
    } 

和控制器

$post = array("class"=>"XI","section"=>"A","stream"=>"Medical"); 
$data = $this->main_model->select("class_subjects",array("class"=>$post['class'],"section"=>"all","stream"=>$post['stream']),$post); 
      print_r($data); 

我沒有得到任何錯誤,但這個是打印表的全部數據。我怎麼能比得上class='XI' and stream='Medical' and (section='A' or section='all')

回答

1

試試這個

function select($table,$match,$or) 
{ 
    $query = $this->db 
     ->select("*") 
     ->from($table) 
     ->group_start() 
      ->where($match) 
     ->group_end() 
     ->group_start() 
      ->or_where($or) 
     ->group_end() 
     ->get(); 

    $strSql = $this->db->last_query(); 

    return $query->result(); 
} 

如果這是行不通的 - 打印出$ STRSQL變量並張貼在這裏查詢。

+0

它爲我工作,感謝您的幫助。 –