我正在尝试将计算出的SQL字段中的值作为别名从我的模型中检索到Controller中。 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'];
}
答案 0 :(得分:0)
试试这个
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;
}
然后你就可以知道哪里犯了错误。