PDO 如何返回已执行查询的总数

发布于 2024-10-07 13:30:47 字数 474 浏览 4 评论 0原文

使用 mysql 包装器

function query($msql)
{
    $this->last_connection =& $this->connection;
    $this->msql =& $msql;
    $queryult = mysql_query($msql, $this->connection);
    if ($queryult) {
        $this->queries_count++;
        return $queryult;
    } else {
        $this->display_errors();
        return false;
    }
}

function num_queries()
{
    return @$this->queries_count;
}

使用 PDO 如何获取已执行查询的总数。

With mysql wrapper

function query($msql)
{
    $this->last_connection =& $this->connection;
    $this->msql =& $msql;
    $queryult = mysql_query($msql, $this->connection);
    if ($queryult) {
        $this->queries_count++;
        return $queryult;
    } else {
        $this->display_errors();
        return false;
    }
}

function num_queries()
{
    return @$this->queries_count;
}

With PDO how to get the total number of executed queries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-10-14 13:30:47

PDO 不计算查询。您必须自己实现它,例如使用 PDO 类的自定义扩展。

PDO does not count queries. You have to implement it yourself e.g. with a custom extension of the PDO class.

恰似旧人归 2024-10-14 13:30:47

如果您正在计算单个进程/请求中的查询,并且需要知道在执行单个任务的过程中执行了多少个查询,则可以使用 _construct 和 _destruct 魔术函数将此功能直接构建到数据库对象中(如果您使用的是 php) 5.3.由于对象中用于查询的现有函数会增加此计数,因此您可以保存它。

编写一个 _destruct 函数,该函数将保存查询函数迭代到系统上的日志文件或数据库中的预定位置的次数。这会将 query_count 属性的值保存到您可以使用它的位置。

function _destruct(){
//write a log file with the value or a serialized version of the database object itself
//set the mode to w+ so it truncates every request
$fh = fopen('/path/to/a/new/log' 'w+');
fwrite($fh, $this->queries_count);
fclose($fh);
}

每次数据库对象被 GC 时都会运行。然后,您可以编写一个静态方法来打开文件并读取值。

public static function getLastQueriesCount(){
  $fh = fopen('/path/to/a/new/log' 'r+');
  $count = fgets($fh);
  fclose($fh);
  return $count;
}

您当然需要为此文件/目录正确设置写入权限。您可以替换函数体来以您喜欢的方式存储计数 - 无论是平面文件格式还是数据库。

If you are counting your queries in a single process/request and need to know how many execute in the course of performing a single task, you could build this functionality right into your database object using the _construct and _destruct magic functions if you are on php 5.3. Since your existing function for querying in your object increments this count, you can save it.

Write a _destruct function that will save how many times the query function has iterated down to a log file on the system or to a predetermined place in the database. This will save the value of the queries_count property out to a place where you can utilize it.

function _destruct(){
//write a log file with the value or a serialized version of the database object itself
//set the mode to w+ so it truncates every request
$fh = fopen('/path/to/a/new/log' 'w+');
fwrite($fh, $this->queries_count);
fclose($fh);
}

This will run every time your database object is GC'ed. You can then write a static method in to open the file and read the value.

public static function getLastQueriesCount(){
  $fh = fopen('/path/to/a/new/log' 'r+');
  $count = fgets($fh);
  fclose($fh);
  return $count;
}

You would of course need write privileges set correctly for this file/directory. You could substitute the function bodies to store the count however you like - be it flat file format or in a database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文