簡體   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