0

我正在使用CodeIgniter 2(最新版本) ......假设我有这个查询:

$CI->db->select("FileName, GUID, Count(GUID)");
$query = $CI->db->get('Files');

我知道我可以使用以下语法获取 GUID 和 FileName 列:

foreach ($query->result() as $row) {
    $file_name = $row->FileName;
}

但我怎样才能得到Count(GUID)

PS。这是一个简单的例子来解释我的问题。我只需要知道如何使用 codeigniter 活动记录获取组合/mysql 函数结果

4

2 回答 2

1

我猜是这个

$CI->db->select("FileName, GUID, Count(GUID) as guidCount");

foreach ($query->result() as $row) {
    $file_name = $row->guidCount;
}
于 2012-10-16T00:27:55.860 回答
1
    $this->db->select('FileName, GUID, Count(GUID) as guidCount');
    $query = $this->db->get('Files');
    // this will call function once and speed up performance not every time in loop
    $rows = $query->result(); 
    foreach ($rows as $row)
    {
       echo $row->FileName;
       echo $row->GUID;
       echo $row->guidCount;
    }

查看更多@ https://www.codeigniter.com/userguide2/database/active_record.html

于 2012-10-16T00:42:59.347 回答