php - Codeigniter事件记录和表别名

标签 php mysql codeigniter

我正在尝试将计算出的SQL字段中的值作为别名从我的模型中检索到控制器中。 Codeigniter在视图中返回NULL,但是当我在Workbench中运行相同的查询时,得到了预期的结果。我的代码有什么地方做错了吗:
这是我的模特

public function get_balance() {
$this->db->select('SUM(IFNULL(transactions.credit,0)-IFNULL(transactions.debit,0)) AS clientBalance',FALSE);
$this->db->from('transactions');
$this->db->where('clientID', $this->uri->segment(3));
$this->db->group_by('accountID');
$query = $this->db->get();
return $query->row_array();
}


还有我的控制器

public function transactions($clientID = NULL) {
        $data['client_balance'] = $this->accounts_model->get_balance();

                $data['client_Balance'] = $data['client_balance']['clientBalance'];
        }   

最佳答案

尝试这个

public function get_balance() {
  $this->db->select('SUM(IFNULL(transactions.credit,0)-IFNULL(transactions.debit,0)) AS clientBalance', FALSE);
  $this->db->from('transactions');
  $this->db->where('clientID', $this->uri->segment(3));
  $this->db->group_by('accountID');
  $query = $this->db->get();
  $result = $query->row_array();
  echo "<pre>".$this->db->last_query();
  die;
  return $result;
}



它将打印您的查询,然后与Workbench查询匹配
要么
在工作台中执行此打印查询


然后,您可以了解哪里出错了。

关于php - Codeigniter事件记录和表别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46083403/

相关文章:

javascript - 连接到 HTML5 应用程序的外部服务器

javascript - 如何同时运行 ajax 调用 - php

php - 如果项目已存在,请勿将其添加到 session 数组中

mysql - 如何连接两个mysql表两次

php - 简单的 SELECT * from ('table_name' ) 失败

php - 按html按钮后如何从mysql数据库中减去?

mySql 数据库 - ID auto_increment : what are the risks of it?

mysql select 不适用于 IN

php - 错误号 : 1064 in codeigniter

php - 无法将 Redis 与 Codeigniter 集成