我有两个连接表;家长和学生。我必须一键单击更新两个表。为此,我想编写一个函数“通过id获取数据”。我设法只为一张表编写该代码。
如果我想从两个表中获取数据,下面的代码怎么写?如果 p_id(父 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->select('*');
$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();
}
其实你可以找到更多这里
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();
}