如何从包含表Id的表中获取所有数据

时间:2014-04-08 05:50:44

标签: php html mysql sql codeigniter

我使用Join编写了以下查询来从不同的表中获取数据。这个查询在MySQL上运行正常,它从数据库中取出所有数据,但是当我用PHP编写这个查询时,它会取出除StudentId之外的所有数据。当我使用print_r时,它会将studentId列显示为空。 请指导我在这里缺少什么?

模型

public function student_profile($id)
{
    $this->db->select('*');
    $this->db->from('student');
    $this->db->join('student_marks',                'student.studentId= student_marks.studentId','left');
    $this->db->join('sports',                       'student.studentId= sports.studentId','left');
    $this->db->join('extra_curricular_activities',  'student.studentId= extra_curricular_activities.studentId','left'   );
    $this->db->where('student.studentId', $id);

    $query=     $this->db->get();

    return $query->result();


    }

控制器

        if ($user_type=='Student')  
        {
            if ($LoginData= $this->loginmodel->studentLogin($username,$password))
            {
                foreach($LoginData as $UserId)
                {
                    $currentId= $UserId->StudentId;                         
                }

                $data['students_data']=         $this->loginmodel->student_profile($currentId);

                $this->load->view('students',$data);

输出

Array
(
    [0] => stdClass Object
        (
            [StudentId] => 
            [StudentName] => Ahmed
            [UserId] => 57502
            [Password] => 28101
            [Batch] => 2009
            [Email] => ahmed@Hotmail.com
            [PhoneNumber] => 02134663028
            [DegreeId] => 1
            [in_semester] => 1
            [StudentMarksId] => 15
            [SemisterCourseId] => 1
            [AssignmentMarks] => 9
            [QuizMarks] => 9
            [ClassTestMarks] => 9
            [FinalMarks] => 45
            [Grades] => F
            [FYPMarks] => 9
            [Attendence] => 
            [GPA] => 0
            [OLQ] => 9
            [InstructorComments] => Satisfactory !!
            [InstructorRatings] => 9
            [Result] => Failed
            [Active] => No
            [SportsId] => 
            [Squash] => 
            [FootBall] => 
            [Cricket] => 
            [Hocky] => 
            [Swimming] => 
            [ExtraActivitiesId] => 
            [DebateCompetition] => 
            [QuizCompetition] => 
            [QiratCompetition] => 
            [DrillCompetition] => 
            [Others] => 
        )

    [1] => stdClass Object
        (
            [StudentId] => 
            [StudentName] => Ahmed
            [UserId] => 57502
            [Password] => 28101
            [Batch] => 2009
            [Email] => ahmed@Hotmail.com
            [PhoneNumber] => 02134663028
            [DegreeId] => 1
            [in_semester] => 1
            [StudentMarksId] => 16
            [SemisterCourseId] => 2
            [AssignmentMarks] => 2
            [QuizMarks] => 2
            [ClassTestMarks] => 2
            [FinalMarks] => 10
            [Grades] => F
            [FYPMarks] => 2
            [Attendence] => 
            [GPA] => 0
            [OLQ] => 2
            [InstructorComments] => 
            [InstructorRatings] => 0
            [Result] => Failed
            [Active] => No
            [SportsId] => 
            [Squash] => 
            [FootBall] => 
            [Cricket] => 
            [Hocky] => 
            [Swimming] => 
            [ExtraActivitiesId] => 
            [DebateCompetition] => 
            [QuizCompetition] => 
            [QiratCompetition] => 
            [DrillCompetition] => 
            [Others] => 
        )

1 个答案:

答案 0 :(得分:0)

因为您正在使用LEFT连接表,并且它们共享studentId的相同列名,所以从mysql中可以从任何连接表中选择最后一列,并且它具有null,您应该使用别名对于您的查询,并为具有相同名称的列提供新的别名,如s.studentId AS student_id

function student_profile($id)
{
    $this->db->select('*,s.studentId AS student_id');
    $this->db->from('student s');
    $this->db->join('student_marks sm', 's.studentId= sm.studentId', 'left');
    $this->db->join('sports sp', 's.studentId= sp.studentId', 'left');
    $this->db->join('extra_curricular_activities e', 's.studentId= e.studentId', 'left');
    $this->db->where('s.studentId', $id);
    $query = $this->db->get();
    return $query->result();
}

当提取结果时,您在[student_id]上有正确的学生ID, 其他方法只需选择所需的列,不要选择*