【问题标题】:CodeIgniter active record, Add IF statement in ->select() functionCodeIgniter 活动记录,在 ->select() 函数中添加 IF 语句
【发布时间】:2011-08-19 16:14:09
【问题描述】:

我有这个问题:

$this->db->select("
    IF(predicts.predict_owner = votes.vote_user_id , IF(judges.judge_did_accept = 1 , True , False) , 'NotExists' )" , 'user_judgement');

我收到语法错误

  `'NotExists'` )

如果我直接在数据库中运行查询,它可以正常工作...
有什么办法可以防止 CI 自动添加符号 `?

谢谢

【问题讨论】:

  • 你在传递什么作为第二个参数?

标签: php codeigniter activerecord


【解决方案1】:

你可以用FALSE作为最后一个参数来调用select方法,像这样

$this->db->select("IF(predicts.predict_owner = votes.vote_user_id , IF(judges.judge_did_accept = 1 , True , False) , 'NotExists' ),'user_judgement'",false);

这将阻止 CI 添加 `

来自User Guide

$this->db->select() 接受可选的第二个参数。如果您将其设置为 FALSE,CodeIgniter 将不会尝试使用反引号保护您的字段或表名称。如果您需要复合选择语句,这很有用。

PS:我看到你用第二个参数调用 select 作为“user_judgement”,我不确定应该怎么做,这不是 cay CI 想要你使用 Active Record

【讨论】:

  • 这对我很有帮助。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 2011-10-22
相关资源
最近更新 更多