简体   繁体   中英

codeigniter select query with multiple where clause

I'm getting an error when I'm using get_where query. I read that it is removed. Can anyone suggest what changes I should make so that I can get values with multiple user defined where clauses.

<?php
 class Select_Model extends CI_Model{       
 public function get_data(){

 if($this->input->post('submit')){

 $value = array('country' => $this->input->post('country'),
    'state' => $this->input->post('state'),
    'city' => $this->input->post('city')
    );

  $query = $this->db->get_where('tablename', $value);

     return $query;

  }}}?>

I'm adding my controller code also here -

class Select_Ctrl extends CI_Controller {

public function index()
{
    $this->load->model('Select_Model');
    $data['val'] = $this->Select_Model->get_data();

    $this->load->view('form',$data);
}}?>

your query is correct just return like this.The result_array() returns your result in array format.For object format use result() in the place of result_array() .

<?php
 class Select_Model extends CI_Model{       
 public function get_data(){

 if($this->input->post('submit')){

 $value = array('country' => $this->input->post('country'),
    'state' => $this->input->post('state'),
    'city' => $this->input->post('city')
    );

  $query = $this->db->get_where('tablename', $value);//not dbname there must be name of your table

     return $query->result_array();

  }}}?>

OR

<?php
 class Select_Model extends CI_Model{       
 public function get_data(){

 if($this->input->post('submit')){

 $this->db->where('country',$this->input->post('country'));
 $this->db->where('state',$this->input->post('state'));
 $this->db->where('city',$this->input->post('city'));


  $query = $this->db->get('tablename');

     return $query->result_array();

  }}}?>

You can use below code for multiple user defined where clause.

<?php
class Select_Model extends CI_Model
{       
    public function get_data()
    {
        if($this->input->post('submit'))
        {
            $this->db->where('country' => $this->input->post('country'));
            $this->db->where('state' => $this->input->post('state'));
            $this->db->where('city' => $this->input->post('city'));
            $query = $this->db->get('tablename');
            return $query->result();
        }
    }
}
?>

From the manual

$this->db->get_where()

Identical to the above function except that it permits you to add a “where” clause in the second parameter, instead of using the db->where() function:

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

Please read the about the where function below for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM