简体   繁体   中英

How to fetch all the data from a table including table Id

I have written the following query using Join to fetch the data from different tables. This query is working fine on MySQL , it fetching out all the data form the database, but when i writ this query in PHP, then it fetches out all the data except StudentId . When i use print_r then it shows studentId column as empty. Kindly guide me what i am missing here?

Model

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();


    }

Controller

        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);

Output

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] => 
        )

Because you are using LEFT join over your tables and they share same column name for studentId so from mysql the last column is picked from your any joined table and it has the null you should use aliases for your query, and give new aliases to columns that has the same name like below 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();
}

When result will be fetched you have a correct student id on [student_id] , Other way to do this just select only needed columns donot select *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM