codeigniter选择具有多个where子句的查询

时间:2017-01-24 10:30:29

标签: php postgresql codeigniter

我在使用get_where查询时遇到错误。我读到它被删除了。任何人都可以建议我应该做出哪些更改,以便我可以获得具有多个用户定义的where子句的值。

<?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;

  }}}?>

我也在这里添加我的控制器代码 -        

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);
}}?>

3 个答案:

答案 0 :(得分:0)

您的查询是正确的,只需按此返回。result_array()以数组格式返回结果。对于对象格式,请在result()的位置使用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();

  }}}?>

答案 1 :(得分:0)

您可以将以下代码用于多个用户定义的where子句。

<?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();
        }
    }
}
?>

答案 2 :(得分:0)

来自手册

$这 - &GT; DB-&GT; get_where()

与上述函数相同,只是它允许您在第二个参数中添加“where”子句,而不是使用db-&gt; where()函数:

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

请阅读以下有关where功能的信息。