您好,我正在尝试检查我的查询是否成功,但它返回NULL值的索引,使得查询似乎成功。如何在我的示例查询中检查查询是否成功,因为我认为它不应该返回NULL索引。
模型
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;
}
}
控制器
$data['class_info'] = $this->staff_model->classmanage_classinfo($section_id);
查看
<p><?php var_dump($class_info) ?></p>
根据我的var_dump,即使索引为空,数组也算作一行?为什么会这样呢?如果查询不成功,我怎么能确保在if语句中触发false。如果有办法改进或解决这个逻辑,那将是非常受欢迎的。感谢。
修改:
完全控制器
$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);
完整模特
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();
}
答案 0 :(得分:-1)
我假设您的查询返回许多行而不是单行。
$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;
}