首页 > 解决方案 > Codeigniter SQL 查询不起作用。它的回报是假的。但是如果我们直接在数据库中运行相同的查询,它会返回结果

问题描述

  $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();

Codeigniter SQL 查询不起作用。它的回报是假的。但是如果我们直接在数据库中运行相同的查询,它会返回结果。

标签: phpcodeigniter

解决方案


你没有->db->where()正确使用。您示例的正确代码点火器方式是:

  $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();


推荐阅读