Count Number of Queries Each Page Load with PDO

前端 未结 2 1284
误落风尘
误落风尘 2021-02-09 09:45

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 h

2条回答
  •  没有蜡笔的小新
    2021-02-09 10:31

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

提交回复
热议问题