[英]Count Number of Queries Each Page Load with PDO
我特意想要以某種方式擴展PDO
對象,以便在每次執行查詢時添加內部計數器。 我希望以某種方式執行此操作,因為我不必更改任何現有查詢或添加此類建議的額外功能。
我不熟悉類或PDO類,但是可以這樣做嗎? 我該怎么做?
我正在考慮擴展query
並execute
每次調用時execute
和增加存儲的變量。
擴展PDO
將像任何其他類一樣完成。 這會滿足你的需求嗎? 唯一的其他代碼更改是在進行初始連接時必須實例化此類而不是PDO
類。
class PDOEx extends PDO
{
private $queryCount = 0;
public function query($query)
{
// Increment the counter.
++$this->queryCount;
// Run the query.
return parent::query($query);
}
public function exec($statement)
{
// Increment the counter.
++$this->queryCount;
// Execute the statement.
return parent::exec($statement);
}
public function GetCount()
{
return $this->queryCount;
}
}
只需執行:
SHOW PROFILES;
最大的Query ID
就是您所追求的。 這還將為您提供有關最近15(默認)查詢執行時間的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.