Count Number of Queries Each Page Load with PDO

匿名 (未验证) 提交于 2019-12-03 02:59:02

问题:

I'm specifically looking to extend the PDO object somehow to add an internal counter every time a query is executed. I want to do this in a way were I wouldn't have to change any of my existing queries or add an extra function like this suggests.

I not that familiar with classes or the PDO class in particular, but could this be done? How would I go about this?

I'm thinking extend query and execute and increment a stored variable each time they're called.

回答1:

Extending PDO would be done like any other class. Would this suit your needs? The only other code change would be having to instantiate this class instead of the PDO class when making your initial connection.

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;     } } 


回答2:

Just execute:

SHOW PROFILES; 

The greatest Query ID is what you are after. This will also give you information about the last 15 (default) query execution time.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!