Codeigniter CI,如何从数据库中通过id获取数据调用?

Codeigniter CI,如何从数据库中通过id获取数据调用?,codeigniter,Codeigniter,+这是我的型号: public function call_by_id($where){ $this->db->select('*'); $this->db->from($this->table); $this->db->where($where); $query = $this->db->get(); return $query->result();

+这是我的型号:

public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->result();
    }
public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('id', $where);
        $query = $this->db->get();
        return $query->result();
    }
+这是我的控制器:

public function index()
    {   
        $data = array();
        $w = '';
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }
public function index()
    {   
        $w = 1; //we need to set value for spesific id
        //we can only call the model's function in controller 
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }
+我想访问view,比如:

$call_id[$id]->username;
// view is just to show the data after we call the model's function in controller
print_r($call_id);
  • 在模型中,您将$放在错误的位置。将其作为参数=>
    按id调用($where)

  • 在控制器中,
    $w
    应该是一个与id值相同的整数,例如:
    $w=1。您不需要编写
    $data=array()

+这是我的型号:

public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->result();
    }
public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('id', $where);
        $query = $this->db->get();
        return $query->result();
    }
+这是我的控制器:

public function index()
    {   
        $data = array();
        $w = '';
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }
public function index()
    {   
        $w = 1; //we need to set value for spesific id
        //we can only call the model's function in controller 
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }
+我想访问view,比如:

$call_id[$id]->username;
// view is just to show the data after we call the model's function in controller
print_r($call_id);

试试看,希望能有所帮助:)

试试这样的方法

public function call_by_id($where){
    $this->db->where('your_column_name', $where);
    $query = $this->db->get($this->table);
    return $query->result_array();
}
在控制器上

public function index()
{   

    $data['results'] = array();

    $w = '1'; // some id

    $data['results'] = $this->mo_product->call_by_id($w);

    $this->load->view('...', $data);
}
看法

仅为一个值更新使用行数组()

模型

控制器

public function index()
{   

    $w = '1'; // some id

    $caller_data = $this->mo_product->call_by_id($w);

    $data['caller_id'] = $caller_data['caller_id'];

    $this->load->view('...', $data);
}
看法



谢谢您的帮助。但还是不行,我跟着你。请帮我更多的忙好吗?感谢您从哪里获得
$w
值?我只是从codeigniter开始。所以我对这个不太了解。然后我向您展示了我的代码,我只想从视图上的语法中获取数据。例如:$call_id['5']->产品名称;我只想这样。谢谢,我怎么才能拿到它?你是说你想调用视图中的
call\u by\u id()
?是的,应该是这样的。但如何从表中获取一些数据呢。我的目的是通过id调用它,但我不知道如何编码?救命啊!!!谢谢,在这种情况下,如果我有5或6个代码,只需显示一个值?