繁体   English   中英

带多个where子句的codeigniter select查询

[英]codeigniter select query with multiple where clause

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

您的查询是正确的,只是这样返回。result_array 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();

  }}}?>

要么

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

  }}}?>

您可以将以下代码用于多个用户定义的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();
        }
    }
}
?>

从手册

$ this-> db-> get_where()

与上面的函数相同,除了它允许您在第二个参数中添加“ where”子句,而不是使用db-> where()函数:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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