在CodeIgniter中获取select语句的空结果

时间:2014-12-12 07:15:06

标签: php mysql sql 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();
        }
   }

视图---&GT; 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>

1 个答案:

答案 0 :(得分:0)

你正在搞乱单引号和双引号。

你需要在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();
        }