2014-09-26 67 views
0

我是cakephp2的新手,我想問如何運行普通的sql查詢並獲得結果,然後 最終將結果設置爲變量?當我運行下面的腳本時,我無法得到結果並將其設置爲變量。如何在cakephp2中運行正常的sql查詢並獲得結果

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

究竟在$結果。你有var_dump這個變量嗎? – 2014-09-26 08:40:16

回答

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; 

希望它可以幫助...

相關問題