【问题标题】:Getting empty result for select statement in CodeIgniter在 CodeIgniter 中获取 select 语句的空结果
【发布时间】:2014-12-12 07:15:06
【问题描述】:

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

这是搜索候选者的控制器

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--->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>

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

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

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

    【讨论】:

    • ,是的,这就是问题所在,现在它可以正常工作了。谢谢
    • 不能投票看看我的声誉:P..我是这个网站上的新人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2017-03-26
    • 2012-12-12
    相关资源
    最近更新 更多