0

Hi there I am trying to check if my query is successful but it is returning indexes of NULL values making it seem that the query is successful. How can I check if the query is success in my sample query because I think it shouldn't return NULL indexes.

Model

 public function classmanage_classinfo($section_id) {
            $query = $this->db->select('students.user_id, students.studentnumber, 
                students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
            ->join('student_section', 'student_section.section_id = sections.section_id', 'left')
            ->join('students', 'students.user_id = student_section.student_id', 'left')
            ->where('sections.section_id', $section_id)
            ->get();

    if ($query->num_rows() > 0) {
        return $query->result();
    } else {
        return false;
    }
 }

Controller

$data['class_info'] = $this->staff_model->classmanage_classinfo($section_id);

View

<p><?php var_dump($class_info) ?></p>

enter image description here

According to my var_dump the array is counted as a row even though the indexes are null? Why is this happening. How could I make sure that if the query isn't successful it triggers false in my if statement. If there are ways to improve or solve this logic it would be most welcome. Thanks.

Edit:

Full controller

$data['class_info'] = $this->staff_model->classmanage_classinfo($section_id);//determinant if query is null is in the model
            $data['section_info'] = $this->staff_model->classmanage_sectioninfo($section_id);

Full Model

public function classmanage_classinfo($section_id) {
        $query = $this->db->select('students.user_id, students.studentnumber, 
            students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
        ->join('student_section', 'student_section.section_id = sections.section_id', 'left')
        ->join('students', 'students.user_id = student_section.student_id', 'left')
        ->where('sections.section_id', $section_id)
        ->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

    public function classmanage_sectioninfo($section_id) {
        //section_name
        $query = $this->db->select('section_name')->where('section_id', $section_id)->get('sections');

        return $query->row();

    }
  • echo $this->db->last_query(); exit; this will print the last executed query try it on phpmyadmin – Anil Shrestha Oct 04 '17 at 02:11
  • I edited the info in my post. The last query i did was `classmanage_sectioninfo` –  Oct 04 '17 at 02:17
  • The record exists even if all the values are NULL. Probably shouldn't let the 'user_id' column allow null values. – DFriend Oct 04 '17 at 02:19
  • what actually you want. is your query not retrieving the data – Anil Shrestha Oct 04 '17 at 02:21
  • No, it is still retrieving data as null even though the record doesn't exist. it should trigger `return false` if the record doesn't exist. –  Oct 04 '17 at 02:23

1 Answers1

-1

I assume that your query returns many rows not a single row.

 $query = $this->db->select('students.user_id, students.studentnumber, 
            students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
        ->join('student_section', 'student_section.section_id = sections.section_id', 'left')
        ->join('students', 'students.user_id = student_section.student_id', 'left')
        ->where('sections.section_id', $section_id)
        ->get()->result();



if (!empty($query)) {
    return $query->result();
} else {
    return false;
}
Anil Shrestha
  • 1,180
  • 11
  • 16
  • 1
    This is correct, `num->rows()` should also be possible, I've also figured out that I should use inner join in my query see here https://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join –  Oct 04 '17 at 02:33