获取 CodeIgniter 中 select 语句的空结果


Getting empty result for select statement in CodeIgniter

在这个项目中,大学必须根据两个参数搜索录取的学生 主题和分数 我已经写了查询,但我得到的结果是空的

这是用于搜索候选的控制器

public function check_search_students()
     {
      $config = array(
                    array(
                            'field' =>'subject',
                            'label' =>'Subject',
                            'rules' =>'required|trim'
                         ),
                    array(
                            'field' =>'mark',
                            'label' =>'Mark',
                            'rules' =>'is_natural'
                        )
                   );
        $this->form_validation->set_error_delimiters('<font color = "red">','</font>');
        $this->form_validation->set_rules($config);
        if($this->form_validation->run()===False)
        {
                $this->search_students();    // another function
        }
        else
        {
            $collegeid=$this->session->userdata('id');
            $info['name'] = $this->college_model->get_student_name();
            $info['search_student']=$this->college_model->search($collegeid);
            $this->load->view('employer/view_search_students',$info);
        }
     }

模范大学模式

public function search($collegeid)
    {
        $title = $this->input->post('subject');
        $mark = $this->input->post('mark');
        $y=0;
        if($mark==0)
        {
        $query=$this->db->query('SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = "$title" ',
                                 array($collegeid)
                               );
         return $query->result_array();
        }
       else
        {   
        $query=$this->db->query('SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = "$title" AND p.mark="$mark"',
                                 array($collegeid)
                               );
        return $query->result_array();
        }
   }

查看--->view_search_students

<table>
         <tr>
         <th> Name </th>
         <th> Mark </th>
    </tr>
         <?php foreach($search_student as $value): ?>
    <tr>
            <td> <?php echo $value['name'] ?> </td>
            <td> <?php echo $value['mark'] ?> </td>
    </tr>
    <?php endforeach ?>
</table>

你弄乱了单引号和双引号。

您需要在SQL中的值周围加上单引号,而不是用双引号引起来。

这就是问题所在。

请检查以下内容:

改变

 if($mark==0)
        {
        $query=$this->db->query('SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = "$title" ',
                                 array($collegeid)
                               );
         return $query->result_array();
        }
       else
        {   
        $query=$this->db->query('SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = "$title" AND p.mark="$mark"',
                                 array($collegeid)
                               );
        return $query->result_array();
        }

 if($mark==0)
        {
        $query=$this->db->query("SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = '$title' ",
                                 array($collegeid)
                               );
         return $query->result_array();
        }
       else
        {   
        $query=$this->db->query("SELECT s.name,s.age,p.sub_title ,s.mark 
                                 FROM applied_subs ae, student s, post_subjects p
                                 WHERE ae.college_id=? and  ae.student_id = s.id
                                 AND p.id=ae.sub_id AND p.sub_title = '$title' AND p.mark='$mark'",
                                 array($collegeid)
                               );
        return $query->result_array();
        }