PDO 如何返回已执行查询的总数
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PDO 不计算查询。您必须自己实现它,例如使用 PDO 类的自定义扩展。
PDO does not count queries. You have to implement it yourself e.g. with a custom extension of the PDO class.
如果您正在计算单个进程/请求中的查询,并且需要知道在执行单个任务的过程中执行了多少个查询,则可以使用 _construct 和 _destruct 魔术函数将此功能直接构建到数据库对象中(如果您使用的是 php) 5.3.由于对象中用于查询的现有函数会增加此计数,因此您可以保存它。
编写一个 _destruct 函数,该函数将保存查询函数迭代到系统上的日志文件或数据库中的预定位置的次数。这会将 query_count 属性的值保存到您可以使用它的位置。
每次数据库对象被 GC 时都会运行。然后,您可以编写一个静态方法来打开文件并读取值。
您当然需要为此文件/目录正确设置写入权限。您可以替换函数体来以您喜欢的方式存储计数 - 无论是平面文件格式还是数据库。
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.
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.
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.