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

oxosxuxt  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(102)
$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

9wbgstp7

9wbgstp71#

您没有正确使用->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();

相关问题