简体   繁体   English

在Codeigniter中使用自定义where子句

[英]Using custom where clause in codeigniter

My active record is 我的活跃记录是

$ql = $this->db->select('course_id')->from('tbl_course_choice')->where('course_id',$course_id)->get();

    if( $ql->num_rows() > 0 ) {
        $data = array(
        'teacher_name_1' => 'mehdi'
        );
        $this->db->where('teacher_name_1', NULL);
        $this->db->where('course_id', $course_id);
        $this->db->update('tbl_course_choice', $data);
    } else {
        $a = array(
            'course_id' => $course_id,
            'course_code' => $course_code,
            'course_title' => $course_title,
            'course_type' => $course_type,
            'credit_hours' => $credit_hours,
            'contact_hours' => $contact_hours,
            'section' => $section,
            'teacher' => $teacher, 
            'pre_requisite' => $pre_requisite,
            'year' => $year,
            'semester' => $semester,
            'offering_year' => $offering_year,
            'offering_session' => $offering_session,
            'teacher_name_1' => $teacher_name_1


            );
        $this->db->insert('tbl_course_choice', $a);
    }

i need to check if filed is null then insert or update else insert in other filed 我需要检查字段是否为空,然后插入或更新,否则插入其他字段

    if {
    $this->db->where('teacher_name_1', NULL);
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $data);
} 
elseif {
    $this->db->where('teacher_name_1', IS NOT NULL);
    $this->db->where('teacher_name_2', NULL);
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $data);
}

How can i do this to check if is null then inset or update. 我该怎么做才能检查是否为null,然后插入或更新。 anyone can help me?? 任何人都可以帮助我吗?

使用自定义字符串

$this->db->where('teacher_name_1 IS NULL', NULL, FALSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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