【问题标题】:Codeigniter: query returns values of NULLCodeigniter:查询返回 NULL 值
【发布时间】:2018-03-15 07:41:47
【问题描述】:

您好,我正在尝试检查我的查询是否成功,但它正在返回 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();

    }

【问题讨论】:

  • echo $this->db->last_query();出口;这将打印最后执行的查询在 phpmyadmin 上尝试
  • 我编辑了帖子中的信息。我做的最后一个查询是classmanage_sectioninfo
  • 即使所有值都为NULL,记录仍然存在。可能不应该让“user_id”列允许空值。
  • 你真正想要的。您的查询是否没有检索数据
  • 不,即使记录不存在,它仍将数据检索为空。如果记录不存在,它应该触发return false

标签: php mysql codeigniter


【解决方案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;
}

【讨论】:

猜你喜欢
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-02
  • 2020-06-20
  • 1970-01-01
  • 2013-04-28
  • 2019-06-14
相关资源
最近更新 更多