传递给 CodeIgniter 的 where() 方法的具有两个条件的字符串会中断查询

问题描述 投票:0回答:1
$this->db->select('sum(commission_amount) as myamount');
$this->db->from('forex_commissions');
$this->db->where('createdDate_sql = `2020-02-16`  and userRef = `MXkIP8z0vs5J`');
$result = $this->db->get();

如果我将查询作为原始 SQL 运行,它可以工作,但是当我尝试将其转录为 CodeIgniter 的 ActiveRecord 方法调用时,它会返回

false

php codeigniter activerecord where-clause
1个回答
1
投票

您没有正确使用

->db->where()
。您的示例的正确 codeigniter 方式是:

  $this->db->select('sum(commission_amount) as myamount');
  $this->db->from('forex_commissions');
  $this->db->where('createdDate_sql', '2020-02-16');
  $this->db->where('userRef', 'MXkIP8z0vs5J');
  $result = $this->db->get();

使用此语法还会自动转义所有值,从而生成更安全的查询。参见这里

注意:您还可以使用以下命令回显最新执行的查询:

echo $this->db->last_query();

© www.soinside.com 2019 - 2024. All rights reserved.