2009-12-08 15 views
0

我一直在我自己的PHP MVC框架。我只想知道如何配置我的框架上運行的所有查詢。我做了其他分析的東西,但我沒有清楚的概念如何配置SQL查詢也爲用戶查看他們是否轉向分析。如何在MVC框架中剖析SQL查詢?

感謝

+0

這將取決於你想要什麼信息。如果您只想要查詢的數量,請在您的代碼中使用相同的實例並在內部存儲該號碼。 – yoda 2009-12-08 12:29:57

+0

這與MVC有什麼關係? – troelskn 2009-12-08 17:46:29

回答

1

這真的取決於你要多少信息,但要有點通用的你,我會考慮的東西這本(類是你用來與數據庫進行交互等等):

<?php 
//Your Database access class 
class DB{ 
    //Create private property for holding profiling data 
    private $sqlDataProfile = array(); 

    function query($sql){ 
     $starttime = time();//Get the time before the query 

     //Query and whatnot here 

     //Start profiling here: 
     $this->addSqlProfile($sql,$starttime); 
    } 

    //Get total number of queries run 
    function getNumberOfQueries(){ 
     return count($this->sqlDataProfile); 
    } 

    private function addSqlProfile($sql,$starttime){ 
     //Create temporary array 
     $tempArr = array(
      'sql'   => $sql, 
      'time'  => time()-$starttime 
     ); 

     //Push tempArr to the sqlDataProfile var 
     $this->sqlDataProfile[] = $tempArr; 
    } 
} 

$db = new DB; 
$db->query('SELECT * FROM table'); 
echo $db->getNumberOfQueries(); 
?> 

這顯然是一個非常簡單的例子,但這種方式既非常簡單,而且在您可以存儲的東西方面也非常強大。例如,您還可以存儲受查詢影響的行數,查詢觸及的表的數量等。

0

Zend Framework是一個很好的例子。您可以在網站下看到SQL查詢。如果你編寫自己的框架,你可以按照這種方式。用戶可以打開或關閉一個布爾變量。如果他們將其設置爲「true」,則可以在網頁下編寫sql查詢。通過該解決方案,編碼人員可以看到MySQL正在運行哪些查詢。