在codeigniter activerecord子查询中添加变量

时间:2013-10-05 12:34:21

标签: sql codeigniter

我试图创建一个活动查询,其中子查询中有一个变量

$fb_UID = $this->session->userdata('fb_UID');

$qs = $this->db->select("*,qs.UID as qs_UID")->
from("question_set qs")->
join("questions q","qs.UID=q.fk_question_set_UID","inner")->
where("sickness_UID",$id)->
where('`qs.UID` NOT IN(SELECT `fk_question_set_UID` FROM `solved_qset` where fk_fb_UID = `$fb_UID`)', NULL, FALSE)->
group_by("qs.UID","asc")->
order_by("qs.UID","random")->
get()->row();

但我收到错误

Unknown column '$fb_UID' in 'where clause'

有没有办法在变量记录子查询中正确放置变量?

感谢

2 个答案:

答案 0 :(得分:0)

试试这段代码。我认为你不能结合很多单引号。如果你在外面使用单引号,你应该在这个单引号内使用双引号,当双引号在外面时它是相同的。

where('"qs.UID" NOT IN(SELECT "fk_question_set_UID" 
FROM "solved_qset" where fk_fb_UID = "$fb_UID")', NULL, FALSE)->// try this....

答案 1 :(得分:0)

请尝试此代码,并连接变量:

$fb_UID = $this->session->userdata('fb_UID');    
$qs = $this->db
     ->select("*,qs.UID as qs_UID")
     ->from("question_set qs")
     ->join("questions q","qs.UID=q.fk_question_set_UID","inner")
     ->where("sickness_UID",$id)
     ->where('`qs.UID` NOT IN(SELECT `fk_question_set_UID`
               FROM `solved_qset` where fk_fb_UID = '.$fb_UID.')', NULL, FALSE)
     ->group_by("qs.UID","asc")
     ->order_by("qs.UID","random")
     ->get()->row();