如何在cakephp2中运行正常的sql查询并获得结果

时间:2014-09-26 07:36:30

标签: php mysql sql cakephp

我是cakephp2的新手,我想问一下如何运行正常的sql查询并获得结果然后 最后将结果设置为变量?当我运行下面的脚本时,我无法获得结果并将其设置为变量。

$results = $this->PvCount->query("SELECT
    dt,
    SUM(total_pv) AS Total
FROM
    pv_counts
WHERE
    curdate()
GROUP BY
    dt");

1 个答案:

答案 0 :(得分:0)

这是一种正确编写的方法:

 $results = $this->PvCount->find
('all',array(
'fields'=>array('PvCount.dt','SUM(PvCount.total_pv) as Total'),
'conditions'=>array('PvCount.date '=>' curdate() '),
'group'=>array('PvCount.dt')
));

从.ctp获取数据:

foreach ($results as $result):
   echo  $result['PvCount']['dt'];
   echo  $result[0]['Total'];
endforeach;
希望它有所帮助...