Home » Php » php – Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns result

php – Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns result

Posted by: admin February 25, 2020 Leave a comment

Questions:
  $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 Query does not work. Its return false. But if we run the same query directly into database its returns result.

How to&Answers:

You are not using ->db->where() properly. The correct codeigniter way of your example is:

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

using this syntax also escapes all values automatically, producing safer queries. see here

note: you can also echo your latest query executed with: echo $this->db->last_query();