我的数据库包含Challenge,Level和Course表格,如下所示:
| ID | course_ID | Level_nbr
| ID | Level_ID | Challenge_nbr |
class course_model extends CI_Model{
function get_level_id($course_id,$level_nbr)
{
$this->db->select('ID');
$this->db->where('Course_ID',$course_id);
$this->db->where('Level_nbr',$level_nbr);
$query = $this->db->get('Level');
return $query->row();
}
function get_challenge_id($course_id,$level_nbr,$challenge_nbr)
{
$level_id=$this->get_level_id($course_id,$level_nbr);
$this->db->select('ID');
$this->db->where('Level_ID',$level_id);
$this->db->where('Challenge_nbr',$challenge_nbr);
$query = $this->db->get('Challenge');
return $query->row();
}
function get_challenge($course_id,$level_nbr,$challenge_nbr)
{
$challenge_id=$this->get_challenge_id($course_id,$level_nbr,$challenge_nbr);
$this->db->select('Title,Text,Points,Init_Code');
$this->db->where('ID',$challenge_id);
$query = $this->db->get(`Challenge`);
return $query->row();
}
}
当我在控制器中调用函数时,就像这样
$query = $this->course_model->get_challenge(1,$level,$challenge)
我有这个错误:
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行的'AND Challenge_nbr
='2'附近使用正确的语法
答案 0 :(得分:2)
使用$query->row()
获得的是一个对象,因此您需要返回该对象的ID属性。
function get_level_id($course_id,$level_nbr)
{
$this->db->select('ID');
$this->db->where('Course_ID',$course_id);
$this->db->where('Level_nbr',$level_nbr);
$query = $this->db->get('Level');
return $query->row()->ID;
}
function get_challenge_id($course_id,$level_nbr,$challenge_nbr)
{
$level_id=$this->get_level_id($course_id,$level_nbr);
$this->db->select('ID');
$this->db->where('Level_ID',$level_id);
$this->db->where('Challenge_nbr',$challenge_nbr);
$query = $this->db->get('Challenge');
return $query->row()->ID;
}
function get_challenge($course_id,$level_nbr,$challenge_nbr)
{
$challenge_id = $this->get_challenge_id($course_id,$level_nbr,$challenge_nbr);
$this->db->select('Title,Text,Points,Init_Code');
$this->db->where('ID',$challenge_id);
$query = $this->db->get('Challenge');
return $query->row(); // this will return you an object too
}