计算每个页面通过PDO加载查询的数量 [英] Count Number of Queries Each Page Load with PDO

查看:90
本文介绍了计算每个页面通过PDO加载查询的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特别希望每次执行查询时都以某种方式扩展PDO对象以添加一个内部计数器.我想以某种方式执行此操作,而不必更改任何现有查询或添加额外的功能

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.

我不太熟悉类,尤其是PDO类,但是可以这样做吗?我该怎么办?

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

我正在考虑扩展queryexecute并在每次调用它们时增加存储的变量.

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

推荐答案

扩展PDO的方式与其他任何类一样.这会适合您的需求吗?唯一的其他代码更改是在建立初始连接时必须实例化该类,而不是PDO类.

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

这篇关于计算每个页面通过PDO加载查询的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆