Codeigniter:如何通过传递 id 使用两个 table 从 id 获取数据

Codeigniter: How to get data from id using two table by passing an id

我有两个连接 table;家长和学生。我必须通过单击一次按钮来更新两个 table。为此,我想编写一个函数 "get data by id"。我设法只为一个 table 编写了该代码。

如果我想从两个table中获取数据,你如何编写下面的代码?如果 p_id (parent id) 是外键?

型号

   function get_by_id($id)
{
    $this->db->from('student');
    $this->db->where('p_id',$id);
    $query = $this->db->get();
    return $query->row();
}

控制器

public function ajax_edit($id)
{
    $data = $this->Model_Action->get_by_id($id);
    echo json_encode($data);
}

嗨,我想你正在找这个。我使用了您代码中的示例:

   function get_by_id($id)
{
    $this->db->from('student');
    $this->db->join('table_b', 'student.p_id=table_b.p_id');
    $this->db->where('student.p_id',$id);
    $query = $this->db->get();
    return $query->row();
}

其实你可以找到更多here

 function get_by_id($id)
{
    $this->db->select('*')
    $this->db->from('student');
    $this->db->join('parent','student.p_id=parent.p_id');
    $this->db->where('student.p_id',$id);
    $query = $this->db->get();
    return $query->row();
}