如何修改 mysql_pconnect 函数以对查询进行计数

发布于 2024-12-04 09:55:18 字数 365 浏览 1 评论 0原文

我有这样的代码:

$hostname = "localhost";
$database = "listings";
$username = "joe";
$password = "1234";

$my_connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

我如何修改函数 $my_connection 以便它增加像 $total_queries 这样的变量,然后它执行正常的 mysql_pconnect() 事情,当然返回相同的事情? 目的是能够在站点页脚中打印:“总查询数:x”。

I have this code:

$hostname = "localhost";
$database = "listings";
$username = "joe";
$password = "1234";

$my_connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

How could I modify the function $my_connection so that it increases a variable like $total_queries and THEN it does the normal mysql_pconnect() thing and of course return the same thing ?
The purpose is to be able to print in site footer: "Total queries: x".

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

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

发布评论

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

评论(2

信仰 2024-12-11 09:55:18

您不必更改 mysql_pconnect() 调用即可计算查询总数。实际上,您不需要在每次查询之前创建新的 mysql 连接 - 如果您一直使用同一个 MySql 服务器 - 您可以(并且在大多数情况下您应该)仅使用一个连接。
回到你的问题:如果你想计算查询总数 - 创建一个函数(或 MySQL 包装类),并将其用于每个查询。在这种情况下,您将能够找出查询总数。下面是此类的模型:

class MySQLWrapper
{

private $_link;
private $_totalQueries;

/**
* Connects to the database server
*
* @param string $hostname
* @param string $username
* @param string $password
*
*/
public function connect($hostname, $username, $password) {
     if (is_null($this->_link)) {
         $this->_link = mysql_pconnect($hostname, $username, $password);
     }
}

/**
* Performs query
*
* @param string $sql
* @return resource
*/
public function query($sql) {
    $this->_totalQueries++;
    return mysql_query($sql, $this->_link);
}

/**
* Returns count of queries made
* @return int
*/
public function getTotalQueryCount() {
    return $this->_totalQueries;
}
}

因此,要找出查询的总数,您可以调用 getTotalQueryCount() 方法。

请注意,这只是真实类的模型,实际代码可能有所不同,但我认为您已经明白了。

You don't have to change the mysql_pconnect() call in order to calculate the total number of queries. Actually, you don't need to create new mysql connection before each query - if you are using the same MySql server all the time - you can (and in most cases you should) use only one connection.
Back to your problem: if you want to calculate the total number of queries - create a function (or a MySQL-wrapper class), and use it for each query. In this case you will be able to find out the total number of queries. Here's the mock-up of such a class:

class MySQLWrapper
{

private $_link;
private $_totalQueries;

/**
* Connects to the database server
*
* @param string $hostname
* @param string $username
* @param string $password
*
*/
public function connect($hostname, $username, $password) {
     if (is_null($this->_link)) {
         $this->_link = mysql_pconnect($hostname, $username, $password);
     }
}

/**
* Performs query
*
* @param string $sql
* @return resource
*/
public function query($sql) {
    $this->_totalQueries++;
    return mysql_query($sql, $this->_link);
}

/**
* Returns count of queries made
* @return int
*/
public function getTotalQueryCount() {
    return $this->_totalQueries;
}
}

Thus, to find out the total number of queries made you can call getTotalQueryCount() method.

Note, this is only a mock-up of the real class, the actual code may be different, but I think you got the idea.

傲影 2024-12-11 09:55:18

您需要为数据库连接/查询执行创建一个抽象层。无论如何,你确实想抽象它,这样你就可以集中处理错误。

例如,如果您创建自己的具有连接和执行函数的类,那么您可以在其中添加所需的任何代码,例如查询计数。您甚至可以让执行函数检查数据库连接是否处于活动状态,如果不活动则建立连接。然后您的 mysql_pconnect 就可以成为查询执行函数的一部分。

顺便说一句,除非您有特定原因在常规连接上使用持久连接,否则不应使用持久连接。这有点违反直觉。
http://php.net/manual/en/features.persistent-connections.php

You need to create an abstraction layer for your database connection/query execution. You really want to abstract it anyway so you have centralized error handling.

For example, if you create you own class that has connect and execute functions, then you can add any code you want in them, like query counting. You can even have your execute function check if the database connection is active and establish a connection if one isn't. Your mysql_pconnect can then be part of the query execution function.

As an aside, persistent connections should not be used unless you have specific reasons to use them over regular connections. It's kind of counterintuitive.
http://php.net/manual/en/features.persistent-connections.php

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