gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:43:58 24 4
gpt4 key购买 nike

我有这个代码:

$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”。

最佳答案

您不必更改 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() 方法。

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

关于php - 如何修改 mysql_pconnect 函数以对查询进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com