【问题标题】:Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns resultCodeigniter SQL 查询不起作用。它的回报是假的。但是如果我们直接在数据库中运行相同的查询,它会返回结果
【发布时间】:2020-06-02 21:26:07
【问题描述】:
  $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 查询不起作用。它的回报是假的。但是如果我们直接在数据库中运行相同的查询,它会返回结果。

【问题讨论】:

  • 值应该用单引号或双引号括起来,而不是反引号。但是使用proper parameterization would be better
  • 我也试过单引号和双引号
  • $this->db->error(); 中有什么内容?

标签: php codeigniter


【解决方案1】:

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

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

注意:您还可以回显您最近执行的查询:echo $this->db->last_query();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多