从DB中获取值然后将其分配给变量后,我无法将其用于下一个查询

时间:2018-01-04 18:16:56

标签: php codeigniter

这是我的模特:

 public function GetStudentData() 
 {
     $sID = $this->input->get('id');
     $class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");   
     $query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
     if($query->num_rows() > 0)
     {
         return $query->row();
     }else{
         return false;
     } 
 }

如何在下一个查询中使用$class_id

1 个答案:

答案 0 :(得分:0)

你可以在一个查询中完成...(来自你的例子[SIC])

    public function GetCSData() {
    $id = $this->input->get('id');      
    $query = $this->db->query("SELECT student.* 
                               FROM student 
                               WHERE student.sID = '$id' 
                               AND student.class_id = (SELECT class_id 
                                                       FROM student 
                                                       WHERE student.sID = '$id')");
    if($query->num_rows() > 0){
        return $query->row();
    }else{
        return false;
    }         

}//..GetCSData end
但是,看着它,它会得到与

相同的结果
SELECT *
FROM student 
WHERE sID = '$id' 

也许我错过了一点?